In what language is 0.1 + 0.2 == 0.3? (I guess C# with decimal literals, but you'd have to specify that with a suffix)
* (= (+ 0.1 0.2) 0.3)
T
* (type-of 0.1)
SINGLE-FLOAT
* (= 0.3d0 (+ 0.1d0 0.2d0))
NIL
* (type-of 0.1d0)
DOUBLE-FLOAT Prelude> (0.1 + 0.2) == 0.3
False
Prelude Ratio> 1 % 10 + 2 % 10 == 3 % 10
True ghci> 0.1 + 0.2 == (0.3 :: Ratio Int)
True
Same type, less syntax.The question of which Simon is left as an exercise to the reader.
0.1 + 0.2 == 0.3 => false
According to "The Ruby Programming Language", all languages that use IEEE-754 floating-point numbers have this problem.
In Ruby you can get around this by using BigDecimal.
>> require 'bigdecimal'
=> true
>> require 'bigdecimal/util'
=> true
>> 0.1.to_d + 0.2.to_d == 0.3.to_d
=> true
>>> decimal.Decimal(".2") + decimal.Decimal(".1") == decimal.Decimal(".3")
True
>>> .2 + .1 == .3
False