README.md (2964B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Incrspace 22 23 > Generate a linearly spaced numeric array using a provided increment. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var incrspace = require( '@stdlib/array/incrspace' ); 31 ``` 32 33 #### incrspace( start, stop\[, increment] ) 34 35 Generates a linearly spaced numeric `array`. If an `increment` is not provided, the default `increment` is `1`. 36 37 ```javascript 38 var arr = incrspace( 0, 11, 2 ); 39 // returns [ 0, 2, 4, 6, 8, 10 ] 40 ``` 41 42 </section> 43 44 <!-- /.usage --> 45 46 <section class="notes"> 47 48 ### Notes 49 50 - The output `array` is guaranteed to include the `start` value but does **not** include the `stop` value. Beware that values subsequent to the `start` value are subject to floating-point errors. Hence, 51 52 ```javascript 53 var arr = incrspace( 0.1, 0.5, 0.2 ); 54 // returns [ 0.1, ~0.3 ] 55 ``` 56 57 where `arr[1]` is only guaranteed to be approximately equal to `0.3`. 58 59 If you desire more control over element precision, consider using [roundn][@stdlib/math/base/special/roundn]: 60 61 ```javascript 62 var roundn = require( '@stdlib/math/base/special/roundn' ); 63 var arr; 64 var out; 65 var i; 66 67 // Create an array subject to floating-point errors: 68 arr = incrspace( 0, 1.01, 0.02 ); 69 70 // Round each value to the nearest hundredth: 71 out = []; 72 for ( i = 0; i < arr.length; i++ ) { 73 out.push( roundn( arr[ i ], -2 ) ); 74 } 75 76 console.log( out.join( '\n' ) ); 77 ``` 78 79 </section> 80 81 <!-- /.notes --> 82 83 <section class="examples"> 84 85 ## Examples 86 87 <!-- eslint no-undef: "error" --> 88 89 ```javascript 90 var incrspace = require( '@stdlib/array/incrspace' ); 91 var out; 92 93 // Default behavior: 94 console.log( '\nDefault:' ); 95 out = incrspace( 0, 10 ); 96 console.log( out.join( '\n' ) ); 97 98 // Specify increment: 99 console.log( '\nIncrement 2:' ); 100 out = incrspace( 0, 10, 2 ); 101 console.log( out.join( '\n' ) ); 102 103 console.log( '\nIncrement 2:' ); 104 out = incrspace( 0, 11, 2 ); 105 console.log( out.join( '\n' ) ); 106 107 console.log( '\nIncrement 0.02:' ); 108 out = incrspace( 0, 1.01, 0.02 ); 109 console.log( out.join( '\n' ) ); 110 111 // Create an array using a negative increment: 112 console.log( '\nDecremented values:' ); 113 out = incrspace( 10, 0, -2 ); 114 console.log( out.join( '\n' ) ); 115 ``` 116 117 </section> 118 119 <!-- /.examples --> 120 121 <section class="links"> 122 123 [@stdlib/math/base/special/roundn]: https://www.npmjs.com/package/@stdlib/math-base-special-roundn 124 125 </section> 126 127 <!-- /.links -->