4 ms·
The real WTF is, why does a person writing some Javascript code need to understand floating point? What high-performance calculations are you doing in you Java
by jluxenberg 17y ago
The real WTF is, why does a person writing some Javascript code need to understand floating point? What high-performance calculations are you doing in you Javascript wouldn't be easier, more accurate, and not that much slower using fixed point math?
- mbrubeck 17y agoWith fixed point or arbitrary-precision math, you either need to (a) set the level of precision explicitly, which is just as bad about forcing the user to understand the underlying model, or (b) have the language choose the level of precision heuristically, and have even more unpredictable performance and rounding characteristics. There's no language that lets you do real-world work with numbers like sqrt(2) or PI without understanding precision and representation to some degree.
- tumult 17y agoJavaScript's brilliant solution is to just not have integers. At all. That's better, somehow?
- mbrubeck 17y agoNo, I think JavaScript's "floats everywhere" is worse than most languages. I prefer languages like Python/Ruby that have one arbitrary-precision integer type and one floating point type, or languages like Haskell or Lisp that have a rich selection of floating/fixed/abitrary/rational types with well-defined performance. But some (not all) of the WTFs here exist in all of those languages too. Eventually, programmers working with numbers really do need to learn how to use different numerical representations.
- godDLL 17y agoCould you please grace us with a code sample for all of these? I'm not intimately familiar with Haskell, Lisp or Ruby. >>> from decimal import * >>> Decimal('0.3') == Decimal('0.1') + Decimal('0.2') True >>> Decimal('NaN') == Decimal('NaN') False >>>
- eslifka 17y agoYou only get that result in python by importing the decimal library. The standard behavior in python is (.1 + .2 == .3) == False However, your code example makes the excellent point that python has such a library available, whereas Javascript does not. At least, not as easily as an import.