Entry
Why is 239.40*100 = 29339.999999999996 in Javascript ? (Try it !)
Jun 17th, 2000 17:09
Mike Hall, Martin T, Martin Honnen, http://support.intel.com/support/processors/pentium/fdiv/
Trying it on several platforms (IE 4.01/5.01, Win 95/98/NT; NS 4.7, Win
95/98/NT) it produces 23940 as expected.
Javascript, like almost all other programming languages, uses native
'floating-point' routines at some point to do mathematical calculations.
These native routines are generally perfomed either by the CPU or a
math-coprocessing chip.
There are always rounding errors associated with floating-point
calculations due to the fact that all numeric values have to be stored
in some fixed number of bits. So expressions such as
2.71894734524524523466451945 may be rounded to fewer decimal places
internally. In this case, the above multiplied by 1000 could result in
2178.954734525 instead of 2718.94734524524523466451945, depending on the
precision of the CPU.
Your equation, however, should be easily handled by any modern hardware
processor. Chances are you have either a browser with a very bad
Javascript engine, or a computer with a defective processor. A few years
ago, Intel released some PII processors that where found to have a
defect in performing floating point operations, producing the kind of
error you see. Check the above URL for help if you think this may be the
case.