I am not a mathematician, and just started programming in javascript and wonders how 2.8 % 2 = 0.7999999999999998.
Note: I know it is remainder operation.
May be I forgot my school mathematics concepts. Can anyone please explain this?
Thanks.
-
1$\begingroup$ What do you mean with modulo? $\endgroup$– Martín Vacas VignoloMar 24, 2016 at 12:37
-
$\begingroup$ That is just a glitch caused by the floating point system. $\endgroup$– Sangchul LeeMar 24, 2016 at 12:39
-
$\begingroup$ Are you wondering why it isn't $0.8$ like it should be or are you confused about the modulo operation? $\endgroup$– NikunjMar 24, 2016 at 12:39
-
$\begingroup$ Are you asking why computers do not represent floating point numbers exactly, or why $2.8 = 0.8\bmod 2$? $\endgroup$– almagestMar 24, 2016 at 12:40
-
$\begingroup$ modulo is remainder operator. here in my question if we divide 2.8 by 2, then we are getting remainder 0.7999999999999998. do you know how? or i think it is programming related doubt. i expect the answer should be 0.8. $\endgroup$– jsinghMar 24, 2016 at 12:41
1 Answer
The floating point number 2.8 cannot be exactly represented in your Javascript floating point number: the binary64 IEEE double precision number is $$2.8 \rightarrow 2.79999999999999982236431605997495353221893310546875\dots$$
-
$\begingroup$ oh thanks a lot, now i got it. answer is simple and clear. $\endgroup$– jsinghMar 24, 2016 at 12:47