Skip to content

Commit 6e2d692

Browse files
oskarlhkvz
authored andcommitted
frexp Math.log2 fallback and removed use of Math.sign (locutusjs#333)
* Math.log2 fallback * Replace use of Math.sign * Frexp 2000 - Now with 100% less semicolons!
1 parent 732bc3b commit 6e2d692

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/c/math/frexp.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ module.exports = function frexp (arg) {
4444

4545
if (arg !== 0 && Number.isFinite(arg)) {
4646
const absArg = Math.abs(arg)
47-
let exp = Math.max(-1023, Math.floor(Math.log2(absArg)) + 1)
47+
// Math.log2 was introduced in ES2015, use it when available
48+
const log2 = Math.log2 || function log2 (n) { return Math.log(n) * Math.LOG2E }
49+
let exp = Math.max(-1023, Math.floor(log2(absArg)) + 1)
4850
let x = absArg * Math.pow(2, -exp)
4951

5052
// These while loops compensate for rounding errors that sometimes occur because of ECMAScript's Math.log2's undefined precision
@@ -58,7 +60,9 @@ module.exports = function frexp (arg) {
5860
exp++
5961
}
6062

61-
x *= Math.sign(arg)
63+
if (arg < 0) {
64+
x = -x
65+
}
6266
result[0] = x
6367
result[1] = exp
6468
}

0 commit comments

Comments
 (0)