-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNumber.js
66 lines (61 loc) · 1.76 KB
/
Number.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Math.trunc = function(num) {
// binary ~~num only works up to 32-bit
return Math[num < 0 ? "ceil" : "floor"](num);
};
Number.prototype.mod = Number.prototype.modulo = function modulo(m) {
// returns this mod m: normalizes this to [0, m[
// JavaScripts % operator normalizes to ]-m, m[ !
var n = this % m;
return n < 0 ? n + m : n;
};
Number.prototype.normRad = function() {
// normalizes a float value to ]-p, p]
return this != Math.pi ? (2 * this) % (2*Math.PI) / 2 : this;
/* var r = this%(2*Math.PI);
if (r>0) {
if (r<=Math.PI) return r;
return r-2*Math.PI;
} else {
if(r>= -1*Math.PI) return r;
return r+2*Math.PI;
} */
};
Number.prototype.posRad = Number.prototype.modulo.pcall(2*Math.PI);
Number.prototype.rad2deg = function() {
return this/Math.PI*180;
};
Number.prototype.deg2rad = function() {
return this*Math.PI/180;
};
Number.prototype.bin = function(len) {
var num = Math.trunc(this), // only works with Integer values
len = Number(len) || 32,
max = Math.pow(2, len-1);
if (isNaN(num) || num >= max || num < -max)
return "NaN";
return num >= 0
? this
.toString(2)
.padleft(len, "0")
: (-num-1) // for len < 32 we could use ~this
.toString(2)
.replace(/[01]/g, function(d){return +!+d;}) // hehe: inverts each char
.padleft(len, "1");
};
Number.prototype.oct = function() {
return this.toString(8);
};
Number.prototype.dec = function() {
return this.toString(10);
};
Number.prototype.hex = function() {
return this.toString(16).toUpperCase();
};
/* Lieber nicht :-)
(function(orig) {
Number.prototype.toString = function toString(base) {
if (typeof base != "number")
throw new Error("Number converted to String without specific base!");
return orig(base);
};
})(Number.prototype.toString); */