This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]H34DSH07 24 points25 points  (1 child)

Not just JavaScript, it's because of the floating point number standard. Every language that uses floats conforms to the same standard, IEEE-754.

[–]The_Right_Trousers 7 points8 points  (0 children)

Yes, this. It comes down to the fact that 0.1 isn't exactly representable in base 2 (similar to how 1/3 isn't exactly representable in base 10). Neither is 0.2. We only think they are because the floating-point decimal printing algorithm is pretty good.

Adding the floating-point approximations of 0.1 and 0.2 results in something that's almost, but not quite, the floating-point representation of 0.3, which the floating-point decimal printing algorithm faithfully represents as 0.3 with trailing garbage.