News for hackers. Stuff that matters.
Re:WTFJS? by jluxenberg on Friday February 12, 2010 @05:47PM (#1122033)
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?
Re:WTFJS? by mbrubeck on Friday February 12, 2010 @05:59PM (#1122061)
With 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.

Re:WTFJS? by tumult on Friday February 12, 2010 @06:20PM (#1122107)
JavaScript's brilliant solution is to just not have integers. At all. That's better, somehow?
Re:WTFJS? by mbrubeck on Friday February 12, 2010 @06:59PM (#1122195)
No, 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.
Re:WTFJS? by godDLL on Saturday February 13, 2010 @12:16PM (#1123194)
Could 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
  >>>
Re:WTFJS? by eslifka on Saturday February 13, 2010 @01:52PM (#1123339)
You 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.