-
Notifications
You must be signed in to change notification settings - Fork 38
/
math.polyfill.js
36 lines (35 loc) · 1.23 KB
/
math.polyfill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Math.trunc()
* version 0.0.0
* Browser Compatibility:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc#browser_compatibility
*/
if (!Math.trunc) {
Math.trunc = function (n) {
return n < 0 ? Math.ceil(n) : Math.floor(n);
};
}
/**
* Math.sign()
* version 0.0.0
* Browser Compatibility:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign#browser_compatibility
*/
if (!Math.sign) {
Math.sign = function (x) {
// If x is NaN, the result is NaN.
// If x is -0, the result is -0.
// If x is +0, the result is +0.
// If x is negative and not -0, the result is -1.
// If x is positive and not +0, the result is +1.
return ((x > 0) - (x < 0)) || +x;
// A more aesthetic pseudo-representation:
//
// ( (x > 0) ? 1 : 0 ) // if x is positive, then positive one
// + // else (because you can't be both - and +)
// ( (x < 0) ? -1 : 0 ) // if x is negative, then negative one
// || // if x is 0, -0, or NaN, or not a number,
// +x // then the result will be x, (or) if x is
// // not a number, then x converts to number
};
}