Closed Bug 356566 Opened 15 years ago Closed 15 years ago

The Javascript modulus arithmetic has gross errors when large numbers are used.

Categories

(Firefox :: General, defect)

x86
Windows Server 2003
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 5856

People

(Reporter: dweesdesign, Unassigned)

References

()

Details

User-Agent:       Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; MathPlayer 2.0; SV1; .NET CLR 1.1.4322)
Build Identifier: Firefox 1.5.0.6

If you do a modulus arithmetic in javascript such as 3848948 % 2 = 0, you see unexpected and completely incorrect results when the number gets too large.  I haven't found the exact boundary where the results are correct and when the results become incorrect.  I noticed this when doing a large number modulus 2, so the problem could be restricted to x%2 only, or it could be symptomatic of a larger problem with the modulus algorithm.

Reproducible: Sometimes

Steps to Reproduce:
1.  Create a script with a modulus arithmetic with a large number mod 2.
2.  Run the script so that you can see the results of the modulus.
3.  Notice that it tells you the incorrect results occasionally when the number is large.

Actual Results:  
Create the following file:
<html>
  <head>
    <title>Modulus bug</title>
    <script type="text/javascript">
       alert(2908390390832333%2);
    </script>
<body>
</body>

Expected Results:  
The box should alert 1 since the number is odd.

The modulus script needs to be reworked so that it is well understood that it will fail for large numbers.  Maybe an alternative more accurate version of Modulus could be provided (with the necessary performance drawbacks to a more complicated script) for when users want more accuracy.
Try the number 43987489047089238383 in the number input box.
JS uses IEEE 754 doubles, which (according to http://en.wikipedia.org/wiki/IEEE_754#Double-precision_64_bit ) has a 52 bit mantissa.  Try asking it to just print 43987489047089238383 ... it shows 43987489047089240000 instead (2^52 is 4503599627370496)

*** This bug has been marked as a duplicate of 5856 ***
Status: UNCONFIRMED → RESOLVED
Closed: 15 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.