2011-11-05 16:46:53 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-10-07 01:04:47 +00:00
|
|
|
/*@
|
|
|
|
Ox.asinh <f> Inverse hyperbolic sine
|
2012-01-04 07:42:48 +00:00
|
|
|
Missing from <code>Math</code>.
|
2012-05-22 23:17:17 +00:00
|
|
|
> Ox.asinh(0)
|
|
|
|
0
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
|
|
|
Ox.asinh = function(x) {
|
|
|
|
return Math.log(x + Math.sqrt(x * x + 1));
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.deg <f> Takes radians, returns degrees
|
2012-01-04 07:42:48 +00:00
|
|
|
Missing from <code>Math</code>.
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.deg(2 * Math.PI)
|
|
|
|
360
|
|
|
|
@*/
|
|
|
|
Ox.deg = function(rad) {
|
|
|
|
return rad * 180 / Math.PI;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.divideInt <f> Divides a number by another and returns an array of integers
|
2012-04-06 00:43:41 +00:00
|
|
|
<code>Ox.divideInt(num, by)</code> returns a sorted array of integers that
|
|
|
|
has a sum of <code>num</code>, a length of <code>by</code>, a minimum of
|
|
|
|
<code>Math.floor(num / by)</code> and a maximum of
|
|
|
|
<code>Math.ceil(num / by)</code>.
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.divideInt(100, 3)
|
|
|
|
[33, 33, 34]
|
|
|
|
> Ox.divideInt(100, 6)
|
|
|
|
[16, 16, 17, 17, 17, 17]
|
|
|
|
@*/
|
2012-04-06 00:43:41 +00:00
|
|
|
// fixme: is splitInt a better name?
|
2011-10-07 01:04:47 +00:00
|
|
|
Ox.divideInt = function(num, by) {
|
2012-04-06 00:43:41 +00:00
|
|
|
var div = parseInt(num / by),
|
2011-10-07 01:04:47 +00:00
|
|
|
mod = num % by;
|
2012-04-06 00:43:41 +00:00
|
|
|
return Ox.range(by).map(function(i) {
|
|
|
|
return div + (i > by - 1 - mod);
|
2011-10-07 01:04:47 +00:00
|
|
|
});
|
2012-04-06 00:43:41 +00:00
|
|
|
};
|
2011-10-07 01:04:47 +00:00
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.limit <f> Limits a number by a given mininum and maximum
|
|
|
|
<code>Ox.limit(num, min, max)</code> is a shorthand for
|
|
|
|
<code>Math.min(Math.max(num, min), max)</code>
|
|
|
|
(num) -> <n> <code>num</code>
|
|
|
|
(num, max) -> <n> <code>Math.max(num, max)</code>
|
|
|
|
(num, min, max) -> <n> <code>Math.min(Math.max(num, min), max)</code>
|
|
|
|
num <n> number
|
|
|
|
min <n> minimum
|
|
|
|
max <n> maximum
|
|
|
|
> Ox.limit(1, 2, 3)
|
|
|
|
2
|
|
|
|
> Ox.limit(4, 2, 3)
|
|
|
|
3
|
|
|
|
> Ox.limit(2, 1)
|
|
|
|
1
|
2012-01-04 07:42:48 +00:00
|
|
|
> Ox.limit(-1, -2)
|
|
|
|
-2
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
|
|
|
Ox.limit = function(/*num[[, min], max]*/) {
|
|
|
|
var len = arguments.length,
|
|
|
|
num = arguments[0],
|
2012-01-04 07:42:48 +00:00
|
|
|
min = len == 3 ? arguments[1] : -Infinity,
|
2011-10-07 01:04:47 +00:00
|
|
|
max = arguments[len - 1];
|
|
|
|
return Math.min(Math.max(num, min), max);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.log <f> Returns the logarithm of a given number to a given base
|
2012-01-04 07:42:48 +00:00
|
|
|
Missing from <code>Math</code>.
|
2011-10-07 01:04:47 +00:00
|
|
|
> 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 <f> Modulo function
|
|
|
|
Unlike <code>-1 % 10</code>, which returns <code>-1</code>,
|
|
|
|
<code>Ox.mod(-1, 10)</code> returns <code>9</code>.
|
|
|
|
> Ox.mod(11, 10)
|
|
|
|
1
|
|
|
|
> Ox.mod(-11, 10)
|
|
|
|
9
|
|
|
|
@*/
|
|
|
|
Ox.mod = function(num, by) {
|
2012-01-04 07:42:48 +00:00
|
|
|
return (num % by + by) % by;
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.rad <f> Takes degrees, returns radians
|
2012-01-04 07:42:48 +00:00
|
|
|
Missing from <code>Math</code>.
|
2011-10-07 01:04:47 +00:00
|
|
|
> Ox.rad(360)
|
|
|
|
2 * Math.PI
|
|
|
|
@*/
|
|
|
|
Ox.rad = function(deg) {
|
|
|
|
return deg * Math.PI / 180;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
2012-01-04 07:42:48 +00:00
|
|
|
Ox.random <f> Returns a random integer within a given range
|
2012-03-30 14:11:29 +00:00
|
|
|
() -> <n> 0 or 1
|
|
|
|
(max) -> <n> Integer between 0 (inclusive) and max (exclusive)
|
|
|
|
(min, max) -> <n> Integer between min (inclusive) and max (exclusive)
|
2012-01-04 07:42:48 +00:00
|
|
|
> [0, 1].indexOf(Ox.random()) > -1
|
|
|
|
true
|
2011-10-07 01:04:47 +00:00
|
|
|
> [0, 1, 2].indexOf(Ox.random(3)) > -1
|
|
|
|
true
|
|
|
|
> Ox.random(1, 2) == 1
|
|
|
|
true
|
|
|
|
@*/
|
|
|
|
Ox.random = function() {
|
|
|
|
var len = arguments.length,
|
2012-01-04 07:42:48 +00:00
|
|
|
min = len == 2 ? arguments[0] : 0,
|
|
|
|
max = len ? arguments[len - 1] : 2;
|
|
|
|
return min + Math.floor(Math.random() * (max - min));
|
2011-10-07 01:04:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*@
|
|
|
|
Ox.round <f> 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 <f> Hyperbolic sine
|
2012-01-04 07:42:48 +00:00
|
|
|
Missing from <code>Math</code>.
|
2012-05-22 23:17:17 +00:00
|
|
|
> Ox.sinh(0)
|
|
|
|
0
|
2011-10-07 01:04:47 +00:00
|
|
|
@*/
|
|
|
|
Ox.sinh = function(x) {
|
|
|
|
return (Math.exp(x) - Math.exp(-x)) / 2;
|
2012-05-22 23:17:17 +00:00
|
|
|
};
|