

Why 0.1 + 0.2 == 0.3 is false? - watermel0n
http://stackoverflow.com/questions/6874357/why-0-1-0-2-0-3

======
JackC
This headline is backwards -- it's the opposite of the actual stackoverflow
question. The question is why, in the D language, `0.1 + 0.2 == 0.3` is _true_
, when it should be false given the underlying floating point implementation.

(Spoiler alert: the answer is that the addition is done at compile time rather
than run time.)

------
dottrap
Douglas Crockford (JavaScript: The Good Parts) has talked about this problem
with binary floating point to do decimal (human) math, on many occasions.

Here is one such instance. [http://www.yuiblog.com/blog/2009/03/10/when-you-
cant-count-o...](http://www.yuiblog.com/blog/2009/03/10/when-you-cant-count-
on-your-numbers/)

He has been on a crusade to introduce a decimal based floating point standard
because he believes it is finally time we revisit this decision to use binary
floating point.

Edit: spelling

------
sheetjs
On a side note, Excel is infuriatingly inconsistent: some operations work in a
way that .1 + .2 == .3 (for example, try `=0.1+0.2=0.3`) but others fall apart
(displayed as a fraction with one digit, 0.3 -> 2/7 but 0.1+0.2 -> 1/3)

~~~
taf2
floating point is hard. backwards compatibility is even harder

~~~
wlievens
For a modern spreadsheet it would not be unreasonable for a user to expect
symbolic arithmetic rather than floating point error soup.

------
pmoriarty
In guile[1]:

    
    
      % guile
      guile> (= (+ 0.1 0.2) 0.3)
      #f
    

[1] -
[https://www.gnu.org/software/guile/](https://www.gnu.org/software/guile/)

~~~
jgrant27
Guile(and most Lisps such as Common Lisp) support rationals specifically to
avoid this problem, why not just use them ?

    
    
      scheme@(guile-user)> (= (+ 1/10 2/10) 3/10)
      $1 = #t

~~~
draegtun
perl6 also use rationals (by default)...

    
    
      $ perl6
    
      > 0.1 + 0.2 == 0.3
      True
    
      > (0.1 + 0.2).perl
      3/10
    

For perl5 you must use the _bigrat_ pragma:

    
    
      $ re.pl
    
      > 0.1 + 0.2 != 0.3
      1
    
      > use bigrat;
      > 0.1 + 0.2 == 0.3
      1
    

ref:
[http://perldoc.perl.org/bigrat.html](http://perldoc.perl.org/bigrat.html)

