"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, updated 2010-02-02)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R103 - Basic Ruby Language Elements [986] puts - opposite of chomp in Ruby - (2006-12-15)
[2287] Learning to program in Ruby - examples of the programming basics - (2009-07-15)
[2296] Variable scope - what is it, and how does it Ruby? - (2009-07-18)
[2613] Constants in Ruby - (2010-02-01)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
[3758] Ruby - standard operators are overloaded. Perl - they are not - (2012-06-09)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4324] Learning to program - variables and constants - (2014-11-22)
[4369] Ruby - the second rung of learning the language - (2014-12-28)
[4504] Where does Ruby load modules from, and how to load from current directory - (2015-06-03)
Q110 - Object Orientation and General technical topics - Programming Algorithms [202] Searching for numbers - (2005-02-04)
[227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[642] How similar are two words - (2006-03-11)
[1157] Speed Networking - a great evening and how we arranged it - (2007-04-21)
[1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1840] Validating Credit Card Numbers - (2008-10-14)
[1949] Nuclear Physics comes to our web site - (2008-12-17)
[2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19)
[2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
[2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
[2586] And and Or illustrated by locks - (2010-01-17)
[2894] Sorting people by their names - (2010-07-29)
[2951] Lots of way of converting 3 letter month abbreviations to numbers - (2010-09-10)
[2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
[3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3451] Why would you want to use a Perl hash? - (2011-09-20)
[3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
[3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
[4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
[4401] Selecting RECENT and POPULAR news and trends for your web site users - (2015-01-19)
[4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
[4410] A good example of recursion - a real use in Python - (2015-02-01)
[4652] Testing new algorithms in PHP - (2016-02-20)
[4656] Identifying the first and last records in a sequence - (2016-02-26)
[4707] Some gems from an introduction to Python - (2016-10-29)
Some other Articles
Ruby collections and strings - some new examplesDirect access to object variable (attributes) in RubyPassing code to procedures and yield in RubyWhat are Ruby Symbols?Comparing floating point numbers - a word of caution and a solutionDefining a static method - Java, Python and RubyString to number conversion with error trapping in RubyNeatly formatting results into a tableThe Model, View, Controller architecture (MVC) - what, why and how.