0

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.

MJD
  • 65,394
  • 39
  • 298
  • 580
jsingh
  • 31
  • 1
    What do you mean with modulo? – Martín Vacas Vignolo Mar 24 '16 at 12:37
  • That is just a glitch caused by the floating point system. – Sangchul Lee Mar 24 '16 at 12:39
  • Are you wondering why it isn't $0.8$ like it should be or are you confused about the modulo operation? – Nikunj Mar 24 '16 at 12:39
  • Are you asking why computers do not represent floating point numbers exactly, or why $2.8 = 0.8\bmod 2$? – almagest Mar 24 '16 at 12:40
  • 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. – jsingh Mar 24 '16 at 12:41
  • Machine precision. – Someone Mar 24 '16 at 12:42
  • yes nikunj, almagest, sangchul got it right, is it because of computer representation? or some math concepts is here? – jsingh Mar 24 '16 at 12:43
  • My answer here might shed some light as well. – Arthur Mar 24 '16 at 12:43
  • Of course this is because of computer representation. However, we can meaningfully discuss how operations between two floating point numbers lose precision, and then mathematics kicks in. – Sangchul Lee Mar 24 '16 at 12:47
  • This is not a maths question. You are apparently asking why some software represents 0.8 as 2.7999999999999998. – almagest Mar 24 '16 at 12:50
  • well i got it that it is related to computer representation, but still i got confused with thought that it could be related to calculation of remainder ( some deep math is followed here) so i asked this question here. Thanks everyone, i got lot new information from you. – jsingh Mar 24 '16 at 12:53
  • It is not really a duplicate, since the linked bug (square roots of integer squares should be exact, esp. if they are powers of 4) is an artifact of the algorithm used, while this problem is caused, as demonstrated in the answer, by the internal binary representation. – Lutz Lehmann Mar 24 '16 at 13:49

1 Answers1

8

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

gammatester
  • 18,827