/*@ Ox.asinh Inverse hyperbolic sine Strangely missing from Math. @*/ Ox.asinh = function(x) { // fixme: no test return Math.log(x + Math.sqrt(x * x + 1)); }; /*@ Ox.deg Takes radians, returns degrees Strangely missing from Math. > Ox.deg(2 * Math.PI) 360 @*/ Ox.deg = function(rad) { return rad * 180 / Math.PI; }; /*@ Ox.divideInt Divides a number by another and returns an array of integers Ox.divideInt(num, by) returns a sorted array of "as equal as possible" integers that has a sum of num and a length of by. > Ox.divideInt(100, 3) [33, 33, 34] > Ox.divideInt(100, 6) [16, 16, 17, 17, 17, 17] @*/ Ox.divideInt = function(num, by) { var arr = [], div = parseInt(num / by), mod = num % by; Ox.loop(by, function(i) { arr[i] = div + (i > by - 1 - mod); }); return arr; } /*@ Ox.limit Limits a number by a given mininum and maximum Ox.limit(num, min, max) is a shorthand for Math.min(Math.max(num, min), max) (num) -> num (num, max) -> Math.max(num, max) (num, min, max) -> Math.min(Math.max(num, min), max) num number min minimum max maximum > Ox.limit(1, 2, 3) 2 > Ox.limit(4, 2, 3) 3 > Ox.limit(2, 1) 1 @*/ Ox.limit = function(/*num[[, min], max]*/) { var len = arguments.length, num = arguments[0], min = len == 3 ? arguments[1] : 0, // fixme: should be -Infinity max = arguments[len - 1]; return Math.min(Math.max(num, min), max); }; /*@ Ox.log Returns the logarithm of a given number to a given base Strangely missing from Math. > Ox.log(100, 10) 2 > Ox.log(Math.E) 1 @*/ Ox.log = function(num, base) { return Math.log(num) / Math.log(base || Math.E); }; /*@ Ox.mod Modulo function Unlike -1 % 10, which returns -1, Ox.mod(-1, 10) returns 9. > Ox.mod(11, 10) 1 > Ox.mod(-11, 10) 9 @*/ Ox.mod = function(num, by) { var mod = num % by; return mod >= 0 ? mod : mod + by; }; /*@ Ox.rad Takes degrees, returns radians Strangely missing from Math. > Ox.rad(360) 2 * Math.PI @*/ Ox.rad = function(deg) { return deg * Math.PI / 180; }; /*@ Ox.random Returns a random integer > [0, 1, 2].indexOf(Ox.random(3)) > -1 true > Ox.random(1, 2) == 1 true @*/ Ox.random = function() { var len = arguments.length, min = len == 1 ? 0 : arguments[0], max = arguments[len - 1]; return min + parseInt(Math.random() * (max - min)); }; /*@ Ox.round Rounds a number with a given number of decimals > Ox.round(2 / 3, 6) 0.666667 > Ox.round(1 / 2, 3) 0.5 > Ox.round(1 / 2) 1 @*/ Ox.round = function(num, dec) { var pow = Math.pow(10, dec || 0); return Math.round(num * pow) / pow; }; /*@ Ox.sinh Hyperbolic sine Strangely missing from Math. @*/ Ox.sinh = function(x) { // fixme: no test return (Math.exp(x) - Math.exp(-x)) / 2; };