|
| 1 | +# Maximum Typed Array Length |
| 2 | + |
| 3 | +> Maximum length for a typed array. |
| 4 | +
|
| 5 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 6 | + |
| 7 | +<section class="intro"> |
| 8 | + |
| 9 | +</section> |
| 10 | + |
| 11 | +<!-- /.intro --> |
| 12 | + |
| 13 | +<!-- Package usage documentation. --> |
| 14 | + |
| 15 | +<section class="usage"> |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +```javascript |
| 20 | +var MAX_TYPEDARRAY_LENGTH = require( '@stdlib/array/constants/max-typedarray-length' ); |
| 21 | +``` |
| 22 | + |
| 23 | +#### MAX_TYPEDARRAY_LENGTH |
| 24 | + |
| 25 | +Maximum length for a typed array. |
| 26 | + |
| 27 | +```javascript |
| 28 | +var len = MAX_TYPEDARRAY_LENGTH; |
| 29 | +// returns 9007199254740991 |
| 30 | +``` |
| 31 | + |
| 32 | +</section> |
| 33 | + |
| 34 | +<!-- /.usage --> |
| 35 | + |
| 36 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 37 | + |
| 38 | +<section class="notes"> |
| 39 | + |
| 40 | +</section> |
| 41 | + |
| 42 | +<!-- /.notes --> |
| 43 | + |
| 44 | +<!-- Package usage examples. --> |
| 45 | + |
| 46 | +<section class="examples"> |
| 47 | + |
| 48 | +## Examples |
| 49 | + |
| 50 | +<!-- eslint-disable no-new-cap --> |
| 51 | + |
| 52 | +```javascript |
| 53 | +var ctors = require( '@stdlib/array/ctors' ); |
| 54 | +var MAX_TYPEDARRAY_LENGTH = require( '@stdlib/array/constants/max-typedarray-length' ); |
| 55 | + |
| 56 | +function fill( dtype, len, value ) { |
| 57 | + var ctor; |
| 58 | + var arr; |
| 59 | + var i; |
| 60 | + if ( len > MAX_TYPEDARRAY_LENGTH ) { |
| 61 | + throw new RangeError( 'invalid input argument. The maximum length for a typed array is '+MAX_TYPEDARRAY_LENGTH+'.' ); |
| 62 | + } |
| 63 | + ctor = ctors( dtype ); |
| 64 | + arr = new ctor( len ); |
| 65 | + for ( i = 0; i < len; i++ ) { |
| 66 | + arr[ i ] = value; |
| 67 | + } |
| 68 | + return arr; |
| 69 | +} |
| 70 | + |
| 71 | +var arr = fill( 'float64', 10, 3.14 ); |
| 72 | +console.log( arr ); |
| 73 | + |
| 74 | +try { |
| 75 | + arr = fill( 'float64', 1e300, 3.14 ); |
| 76 | +} catch ( err ) { |
| 77 | + console.error( err.message ); |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +</section> |
| 82 | + |
| 83 | +<!-- /.examples --> |
| 84 | + |
| 85 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 86 | + |
| 87 | +<section class="references"> |
| 88 | + |
| 89 | +</section> |
| 90 | + |
| 91 | +<!-- /.references --> |
| 92 | + |
| 93 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 94 | + |
| 95 | +<section class="links"> |
| 96 | + |
| 97 | +</section> |
| 98 | + |
| 99 | +<!-- /.links --> |
0 commit comments