0

I was experimenting with floating points, and I came across something weird. I did

8.00000000000000100000.toFixed(20)

in the console and got

8.00000000000000177636

How is this possible? I heard floating points round, but this doesn't seem rounded. Does it have to do with the conversion to a string in toFixed?

Cilan
  • 13,101
  • 3
  • 34
  • 51

1 Answers1

3

From Wikipedia:

Whether or not a rational number has a terminating expansion depends on the base. For example, in base-10 the number 1/2 has a terminating expansion (0.5) while the number 1/3 does not (0.333...). In base-2 only rationals with denominators that are powers of 2 (such as 1/2 or 3/16) are terminating. Any rational with a denominator that has a prime factor other than 2 will have an infinite binary expansion. This means that numbers which appear to be short and exact when written in decimal format may need to be approximated when converted to binary floating-point.

spender
  • 117,338
  • 33
  • 229
  • 351