Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Math.atanh #1438

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/NativeMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private static Object atanh(Context cx, Scriptable scope, Scriptable thisObj, Ob
}
return ScriptRuntime.negativeZeroObj;
}
return Double.valueOf(0.5 * Math.log((x + 1.0) / (x - 1.0)));
return Double.valueOf(0.5 * Math.log((x + 1.0) / (1.0 - x)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe pedantic but to be in sync with MDN

return Double.valueOf(0.5 * Math.log((1.0 + x) / (1.0 - x)));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
return ScriptRuntime.NaNobj;
}
Expand Down
74 changes: 74 additions & 0 deletions testsrc/jstests/harmony/math-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,78 @@ assertEquals(Math.imul(-2, -2), 4);
assertEquals(Math.imul(0xffffffff, 5), -5);
assertEquals(Math.imul(0xfffffffe, 5), -10);

assertEqualsDelta(Math.atanh(1/2), 0.549306144334059, 0.0000000000001);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the 262 tests suite seems to have no tests for this maybe you can add some more samples

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

assertEquals(Math.atanh(0), 0);
assertEquals(Math.atanh(-0), -0);
assertEquals(Math.atanh(1), Infinity);
assertEquals(Math.atanh(-1), -Infinity);
assertEquals(Math.atanh(Infinity), NaN);
assertEquals(Math.atanh(-Infinity), NaN);
assertEquals(Math.atanh(NaN), NaN);
assertEquals(Math.atanh('foo'), NaN);
assertEquals(Math.atanh(), NaN);

assertEqualsDelta(Math.asinh(1), 0.8813735870195429, 0.0000000000001);
assertEqualsDelta(Math.asinh(-1/2), -0.48121182505960336, 0.0000000000001);
assertEquals(Math.asinh(0), 0);
assertEquals(Math.asinh(-0), -0);
assertEquals(Math.asinh(Infinity), Infinity);
assertEquals(Math.asinh(-Infinity), -Infinity);
assertEquals(Math.asinh(NaN), NaN);
assertEquals(Math.asinh('foo'), NaN);
assertEquals(Math.asinh(), NaN);

assertEquals(Math.acosh(1), 0);
assertEquals(Math.acosh(-1), NaN);
assertEqualsDelta(Math.acosh(2), 1.3169578969248166, 0.0000000000001);
assertEquals(Math.acosh(0), NaN);
assertEquals(Math.acosh(-0), NaN);
assertEquals(Math.acosh(Infinity), Infinity);
assertEquals(Math.acosh(-Infinity), NaN);
assertEquals(Math.acosh(NaN), NaN);
assertEquals(Math.acosh('foo'), NaN);
assertEquals(Math.acosh(), NaN);

assertEquals(Math.log2(1), 0);
assertEquals(Math.log2(2), 1);
assertEqualsDelta(Math.log2(3), 1.584962500721156, 0.0000000000001);
assertEquals(Math.log2(0), -Infinity);
assertEquals(Math.log2(-0), -Infinity);
assertEquals(Math.log2(-2), NaN);
assertEquals(Math.log2(NaN), NaN);
assertEquals(Math.log2('foo'), NaN);
assertEquals(Math.log2(), NaN);

assertEquals(Math.sign(1), 1);
assertEquals(Math.sign(2), 1);
assertEquals(Math.sign(-3), -1);
assertEquals(Math.sign(0), 0);
assertEquals(Math.sign(-0), -0);
assertEquals(Math.sign(Infinity), 1);
assertEquals(Math.sign(-Infinity), -1);
assertEquals(Math.sign(NaN), NaN);
assertEquals(Math.sign('foo'), NaN);
assertEquals(Math.sign(), NaN);

assertEquals(Math.clz32(0), 32);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a separate PR #1430 for clz32, maybe we have to merge the two different approaches of tests for the functions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed all the cases from this file - it seems the other PR will add complete coverage

assertEquals(Math.clz32(1), 31);
assertEquals(Math.clz32(2), 30);
assertEquals(Math.clz32(-0), 32);
assertEquals(Math.clz32(-2), 0);
assertEquals(Math.clz32(Infinity), 32);
assertEquals(Math.clz32(-Infinity), 32);
assertEquals(Math.clz32(NaN), 32);
assertEquals(Math.clz32(NaN), 32);
assertEquals(Math.clz32('foo'), 32);
assertEquals(Math.clz32(), 32);

assertEquals(Math.fround(0.5), 0.5);
assertEquals(Math.fround(5.4), 5.400000095367432);
assertEquals(Math.fround(-2.2), -2.200000047683716);
assertEquals(Math.fround(Infinity), Infinity);
assertEquals(Math.fround(-Infinity), -Infinity);
assertEquals(Math.fround(NaN), NaN);
assertEquals(Math.fround('x'), NaN);
assertEquals(Math.fround(), NaN);

"success";