improve implementation and documentation of Ox.divideInt

This commit is contained in:
rolux 2012-04-06 02:43:41 +02:00
parent adb51d67a1
commit 24eb4bdc64

View file

@ -21,23 +21,23 @@ Ox.deg = function(rad) {
/*@
Ox.divideInt <f> Divides a number by another and returns an array of integers
<code>Ox.divideInt(num, by)</code> returns a sorted array of "as equal as
possible" integers that has a sum of <code>num</code> and a length of
<code>by</code>.
<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>.
> Ox.divideInt(100, 3)
[33, 33, 34]
> Ox.divideInt(100, 6)
[16, 16, 17, 17, 17, 17]
@*/
// fixme: is splitInt a better name?
Ox.divideInt = function(num, by) {
var arr = [],
div = parseInt(num / by),
var div = parseInt(num / by),
mod = num % by;
Ox.loop(by, function(i) {
arr[i] = div + (i > by - 1 - mod);
return Ox.range(by).map(function(i) {
return div + (i > by - 1 - mod);
});
return arr;
}
};
/*@
Ox.limit <f> Limits a number by a given mininum and maximum