views:

334

answers:

2

Duplicate:


trace( ">> " + (399.6-(Math.floor(399.6))) );

prints out

>> 0.6000000000000227

why?

+3  A: 

Floating Point rounding errors. Floating point numbers can't represent some values exactly, so what you are seeing is the limitations of using FP numbers. The mantissa (the part of the FP num that gives the accuracy) is only a certain number of bits, and when something can't be exactly represented, you get results like the above. I know for a fact that 0.1 can't be represented exactly, so it makes sense that 0.6 can't as well.

Scott M.
A: 

Essentially, the decimal part of a floating point number is a summation:

1/2 + 1/4 + 1/8 + ...

where each fraction is one bit of your floating point representation. As paintballbob mentioned, this means there are some numbers that can't be represented (such at 0.1).

As far as I know, there is no fixed-point decimal built in to AS3 (though there are third-party libraries).

Travis Jensen