-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
88 lines (77 loc) · 1.67 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
*
* COMPUTE: logspace
*
*
* DESCRIPTION:
* - Generates a logarithmically spaced numeric array.
*
*
* NOTES:
* [1]
*
*
* TODO:
* [1]
*
*
* LICENSE:
* MIT
*
* Copyright (c) 2014-2015. Athan Reines.
*
*
* AUTHOR:
* Athan Reines. kgryte@gmail.com. 2014.
*
*/
'use strict';
// MODULES //
var isInteger = require( 'validate.io-integer' );
// LOGSPACE //
/**
* FUNCTION: logspace( a, b[, length] )
* Generates a logarithmically spaced numeric array.
*
* @param {Number} a - exponent of start value
* @param {Number} b - exponent of end value
* @param {Number} [length] - length of output array (default: 10)
* @returns {Array} logarithmically spaced numeric array
*/
function logspace( a, b, len ) {
var arr,
end,
tmp,
d;
if ( typeof a !== 'number' || a !== a ) {
throw new TypeError( 'logspace()::invalid input argument. Exponent of start value must be numeric. Value: `' + a + '`.' );
}
if ( typeof b !== 'number' || b !== b ) {
throw new TypeError( 'logspace()::invalid input argument. Exponent of stop value must be numeric. Value: `' + b + '`.' );
}
if ( arguments.length < 3 ) {
len = 10;
} else {
if ( !isInteger( len ) || len < 0 ) {
throw new TypeError( 'logspace()::invalid input argument. Length must be a positive integer. Value: `' + len + '`.' );
}
if ( len === 0 ) {
return [];
}
}
// Calculate the increment:
end = len - 1;
d = ( b-a ) / end;
// Build the output array...
arr = new Array( len );
tmp = a;
arr[ 0 ] = Math.pow( 10, tmp );
for ( var i = 1; i < end; i++ ) {
tmp += d;
arr[ i ] = Math.pow( 10, tmp );
}
arr[ end ] = Math.pow( 10, b );
return arr;
} // end FUNCTION logspace()
// EXPORTS //
module.exports = logspace;