Closed Bug 1070517 Opened 10 years ago Closed 8 years ago

Math.log2 should be exact for exact powers of 2

Categories

(Core :: JavaScript: Standard Library, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla52
Tracking Status
firefox52 --- fixed

People

(Reporter: anba, Assigned: arai)

References

Details

Attachments

(1 file)

From es-discuss: https://mail.mozilla.org/pipermail/es-discuss/2014-September/039470.html
Related v8 issue: https://code.google.com/p/v8/issues/detail?id=3579

Expected: No output
Actual: 441 powers of two have inexact results, e.g. `Math.log2(Math.pow(2, 124))` outputs `124.00000000000001` instead of `124`

Reproducible on Win7 32bit, Fx32. Depends on whether or not log2 is supported by the compiler, see jsmath.cpp.


for (var k = -1074; k <= 1023; ++k) {
  if (Math.log2(Math.pow(2, k)) != k) {
    console.log("Math.log2(2^" + k + "): "+ Math.log2(Math.pow(2, k)))
  }
}
  // extracts the exponent from the double value
  Math.getExponent = function (x) {
    var e = Math.floor(Math.log(x) / Math.log(2) + 0.5);
    return Math.pow(2, e) > x ? e - 1 : e;
  };

  Math.log2 = function (x) {
    if (x === 0) {
      return -1 / 0;
    }
    if (x === 1 / 0) {
      return 1 / 0;
    }
    var e = Math.getExponent(x);
    if (e < 0) {
      e += 1;
    }
    return Math.log(x / Math.pow(2, e)) / Math.log(2) + e;
  };
Now the testcase in comment #0 don't show anything, on Winxp 32bit nightly (2016-07-04),
as bug 933257 changed the implementation to fdlibm.

we could add the testcase and close this bug :)
See Also: → 933257
Just increased the range.
it passes on all archs
https://treeherder.mozilla.org/#/jobs?repo=try&revision=b247807c029b
Assignee: nobody → arai.unmht
Attachment #8769297 - Flags: review?(jorendorff)
Attachment #8769297 - Flags: review?(jorendorff) → review?(till)
Comment on attachment 8769297 [details] [diff] [review]
Increase the testing range for Math.log2 + Math.pow.

Review of attachment 8769297 [details] [diff] [review]:
-----------------------------------------------------------------

Bleh, sorry for the long delay - I somehow didn't see this :(
Attachment #8769297 - Flags: review?(till) → review+
https://hg.mozilla.org/mozilla-central/rev/016d82db1c6b
Status: NEW → RESOLVED
Closed: 8 years ago
Flags: in-testsuite+
Resolution: --- → FIXED
Target Milestone: --- → mozilla52
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: