|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 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 | +# Minimum Data Type |
| 22 | + |
| 23 | +> Determine the minimum ndarray [data type][@stdlib/ndarray/dtypes] for storing a provided signed integer value. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var minSignedIntegerDataType = require( '@stdlib/ndarray/base/min-signed-integer-dtype' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### minSignedIntegerDataType( value ) |
| 44 | + |
| 45 | +Returns the minimum ndarray [data type][@stdlib/ndarray/dtypes] for storing a provided signed integer value. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var dt = minSignedIntegerDataType( 9999 ); |
| 49 | +// returns 'int16' |
| 50 | + |
| 51 | +dt = minSignedIntegerDataType( -3 ); |
| 52 | +// returns 'int8' |
| 53 | + |
| 54 | +dt = minSignedIntegerDataType( 3 ); |
| 55 | +// returns 'int8' |
| 56 | + |
| 57 | +dt = minSignedIntegerDataType( 1e100 ); |
| 58 | +// returns 'float64' |
| 59 | +``` |
| 60 | + |
| 61 | +</section> |
| 62 | + |
| 63 | +<!-- /.usage --> |
| 64 | + |
| 65 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 66 | + |
| 67 | +<section class="notes"> |
| 68 | + |
| 69 | +## Notes |
| 70 | + |
| 71 | +- Once a provided integer value exceeds the maximum values of all supported signed integer [data types][@stdlib/ndarray/dtypes], the function defaults to returning `'float64'`. |
| 72 | + |
| 73 | +</section> |
| 74 | + |
| 75 | +<!-- /.notes --> |
| 76 | + |
| 77 | +<!-- Package usage examples. --> |
| 78 | + |
| 79 | +<section class="examples"> |
| 80 | + |
| 81 | +## Examples |
| 82 | + |
| 83 | +<!-- eslint no-undef: "error" --> |
| 84 | + |
| 85 | +```javascript |
| 86 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 87 | +var exp2 = require( '@stdlib/math/base/special/exp2' ); |
| 88 | +var minSignedIntegerDataType = require( '@stdlib/ndarray/base/min-signed-integer-dtype' ); |
| 89 | + |
| 90 | +// Generate random powers: |
| 91 | +var exp = discreteUniform( 100, 0, 40, { |
| 92 | + 'dtype': 'generic' |
| 93 | +}); |
| 94 | + |
| 95 | +// Determine the minimum data type for each generated value... |
| 96 | +var v; |
| 97 | +var i; |
| 98 | +for ( i = 0; i < exp.length; i++ ) { |
| 99 | + v = exp2( exp[ i ] ); |
| 100 | + console.log( 'min(%d) => %s', v, minSignedIntegerDataType( v ) ); |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +</section> |
| 105 | + |
| 106 | +<!-- /.examples --> |
| 107 | + |
| 108 | +<!-- 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. --> |
| 109 | + |
| 110 | +<section class="references"> |
| 111 | + |
| 112 | +</section> |
| 113 | + |
| 114 | +<!-- /.references --> |
| 115 | + |
| 116 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 117 | + |
| 118 | +<section class="related"> |
| 119 | + |
| 120 | +</section> |
| 121 | + |
| 122 | +<!-- /.related --> |
| 123 | + |
| 124 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 125 | + |
| 126 | +<section class="links"> |
| 127 | + |
| 128 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/ndarray/tree/main/dtypes |
| 129 | + |
| 130 | +</section> |
| 131 | + |
| 132 | +<!-- /.links --> |
0 commit comments