5 ms·
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)
by waldrews 17y ago
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)
- twopoint718 17y agoI was curious and fired up sbcl: * (= (+ 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
- techiferous 17y agoIn Ruby: 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
- martian 17y agoIn Python >>> decimal.Decimal(".2") + decimal.Decimal(".1") == decimal.Decimal(".3") True >>> .2 + .1 == .3 False
- Locke1689 17y agoPrelude> (0.1 + 0.2) == 0.3 False Prelude Ratio> 1 % 10 + 2 % 10 == 3 % 10 True
- jrockway 17y agoOr: ghci> 0.1 + 0.2 == (0.3 :: Ratio Int) True Same type, less syntax.
- regularfry 17y agoThat's cheating. Haskell isn't a language, it's The Word of Simon. The question of which Simon is left as an exercise to the reader.