time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

incrspace.js (2406B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var ceil = require( '@stdlib/math/base/special/ceil' );
     24 var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
     25 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     26 var MAX_LENGTH = require( '@stdlib/constants/uint32/max' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Generates a linearly spaced numeric array using a provided increment.
     33 *
     34 * @param {number} x1 - first array value
     35 * @param {number} x2 - array element bound
     36 * @param {number} [increment=1] - increment
     37 * @throws {TypeError} first argument must be numeric
     38 * @throws {TypeError} second argument must be numeric
     39 * @throws {TypeError} third argument must be numeric
     40 * @throws {RangeError} length of created array must be less than `4294967295` (`2**32 - 1`)
     41 * @returns {Array} linearly spaced numeric array
     42 *
     43 * @example
     44 * var arr = incrspace( 0, 11, 2 );
     45 * // returns [ 0, 2, 4, 6, 8, 10 ]
     46 */
     47 function incrspace( x1, x2, increment ) {
     48 	var arr;
     49 	var len;
     50 	var inc;
     51 	var i;
     52 	if ( !isNumber( x1 ) || isnan( x1 ) ) {
     53 		throw new TypeError( 'invalid argument. Start must be numeric. Value: `' + x1 + '`.' );
     54 	}
     55 	if ( !isNumber( x2 ) || isnan( x2 ) ) {
     56 		throw new TypeError( 'invalid argument. Stop must be numeric. Value: `' + x2 + '`.' );
     57 	}
     58 	if ( arguments.length < 3 ) {
     59 		inc = 1;
     60 	} else {
     61 		inc = increment;
     62 		if ( !isNumber( inc ) || isnan( inc ) ) {
     63 			throw new TypeError( 'invalid argument. Increment must be numeric. Value: `' + inc + '`.' );
     64 		}
     65 	}
     66 	len = ceil( ( x2-x1 ) / inc );
     67 
     68 	if ( len > MAX_LENGTH ) {
     69 		throw new RangeError( 'invalid arguments. Generated array exceeds maximum array length.' );
     70 	}
     71 	if ( len <= 1 ) {
     72 		return [ x1 ];
     73 	}
     74 	arr = [];
     75 	arr.push( x1 );
     76 	for ( i = 1; i < len; i++ ) {
     77 		arr.push( x1 + (inc*i) );
     78 	}
     79 	return arr;
     80 }
     81 
     82 
     83 // EXPORTS //
     84 
     85 module.exports = incrspace;