0
$\begingroup$

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.

$\endgroup$
12
  • 1
    $\begingroup$ What do you mean with modulo? $\endgroup$ Mar 24, 2016 at 12:37
  • $\begingroup$ That is just a glitch caused by the floating point system. $\endgroup$ Mar 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$
    – Nikunj
    Mar 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$
    – almagest
    Mar 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$
    – jsingh
    Mar 24, 2016 at 12:41

1 Answer 1

8
$\begingroup$

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$$

$\endgroup$
1
  • $\begingroup$ oh thanks a lot, now i got it. answer is simple and clear. $\endgroup$
    – jsingh
    Mar 24, 2016 at 12:47

Not the answer you're looking for? Browse other questions tagged .