Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Comparing floating point numbers - a word of caution and a solution

"Think of a number between 5 and 15.
Double it.
Add fourteen.
Halve it.
Take away the number you first thought of.
Is the result 7?"

We all played games like that as children, thinking of the number 0 ourselves so that we could do an easy calculation and impress our friends. And the result was 7 ... whether you started from 0, 5, 7, 9, 13, 15 ... or any other value.

But if you try such things with floating point numbers in more or less any programming language, you'll not necessarily find that you get back to an exact number, or the number you first thought of. And that's because computers store floating point numbers in a format where they are ever so slighly rounded.

The issue is a common one to most languages, but it happened to come up today on a Ruby course, so I've written an example in Ruby to show the problem. I took a number, divided it, added, multiplied, subtracted, and tested to see if I was back where I started. It wasn't - 1.0 had become 0.999999999999993 and When I tested the result to see if it was equal to one, my program declared that it was NOT!

Be careful when testing floating point numbers in any language - you'll do far better to work with integers much / most of the time. And if you must test a floating point variable on which you have done significant calculations for equality, you'll be well advised to do so by checking to see how much the absolute difference between the two values is, as a proportion of one of the values that you're comparing. Does this sound complex? It isn't, and it can easily be implements as a function:

def approx?(this,that)
  diff = (this-that).abs
  return diff < (this * 0.0000000001)
end


The full source code of my example, together with some test output, is [here].

(written 2010-02-01 22:56:38)

 
Associated topics are indexed under
R103 - Basic Ruby Language Elements
  [2613] Constants in Ruby - (2010-02-01)
  [2296] Variable scope - what is it, and how does it Ruby? - (2009-07-18)
  [2287] Learning to program in Ruby - examples of the programming basics - (2009-07-15)
  [986] puts - opposite of chomp in Ruby - (2006-12-15)

Q110 - Object Orientation and General technical topics - Programming Algorithms
  [2586] And and Or illustrated by locks - (2010-01-17)
  [2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
  [2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
  [2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19)
  [1949] Nuclear Physics comes to our web site - (2008-12-17)
  [1840] Validating Credit Card Numbers - (2008-10-14)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
  [1157] Speed Networking - a great evening and how we arranged it - (2007-04-21)
  [642] How similar are two words - (2006-03-11)
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
  [202] Searching for numbers - (2005-02-04)


Back to
Defining a static method - Java, Python and Ruby
Previous and next
or
Horse's mouth home
Forward to
What are Ruby Symbols?
Some other Articles
Ruby collections and strings - some new examples
Direct access to object variable (attributes) in Ruby
Passing code to procedures and yield in Ruby
What are Ruby Symbols?
Comparing floating point numbers - a word of caution and a solution
Defining a static method - Java, Python and Ruby
String to number conversion with error trapping in Ruby
Neatly formatting results into a table
The Model, View, Controller architecture (MVC) - what, why and how.
2676 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2010: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 344596 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho