Skip to content

Commit c3bffe5

Browse files
committed
feat: add ndarray/base/min-signed-integer-dtype
1 parent 02fe522 commit c3bffe5

File tree

10 files changed

+605
-0
lines changed

10 files changed

+605
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
129+
130+
</section>
131+
132+
<!-- /.links -->
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 bench = require( '@stdlib/bench' );
24+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var pkg = require( './../package.json' ).name;
26+
var minSignedIntegerDataType = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var out;
34+
var N;
35+
var i;
36+
37+
values = [
38+
0,
39+
1,
40+
-128,
41+
128,
42+
99999,
43+
-99999,
44+
1.0e100,
45+
-1.0e100,
46+
-1234567890,
47+
1234567890
48+
];
49+
N = values.length;
50+
51+
b.tic();
52+
for ( i = 0; i < b.iterations; i++ ) {
53+
out = minSignedIntegerDataType( values[ i%N ] );
54+
if ( typeof out !== 'string' ) {
55+
b.fail( 'should return a string' );
56+
}
57+
}
58+
b.toc();
59+
if ( !isString( out ) ) {
60+
b.fail( 'should return a string' );
61+
}
62+
b.pass( 'benchmark finished' );
63+
b.end();
64+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
{{alias}}( value )
3+
Returns the minimum ndarray data type for storing a provided signed integer
4+
value.
5+
6+
Parameters
7+
----------
8+
value: number
9+
Signed integer value.
10+
11+
Returns
12+
-------
13+
dt: string
14+
ndarray data type.
15+
16+
Examples
17+
--------
18+
> var dt = {{alias}}( 3 )
19+
'int8'
20+
> dt = {{alias}}( -3 )
21+
'int8'
22+
> dt = {{alias}}( 1280 )
23+
'int16'
24+
25+
See Also
26+
--------
27+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { SignedIntegerDataType } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Output data type.
27+
*/
28+
type DataType = SignedIntegerDataType | 'float64';
29+
30+
/**
31+
* Returns the minimum ndarray data type for storing a provided signed integer value.
32+
*
33+
* @param value - scalar value
34+
* @returns ndarray data type
35+
*
36+
* @example
37+
* var dt = minSignedIntegerDataType( 1280 );
38+
* // returns 'int16'
39+
*
40+
* @example
41+
* var dt = minSignedIntegerDataType( 3 );
42+
* // returns 'int8'
43+
*/
44+
declare function minSignedIntegerDataType( value: number ): DataType;
45+
46+
47+
// EXPORTS //
48+
49+
export = minSignedIntegerDataType;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
import minSignedIntegerDataType = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a data type..
25+
{
26+
minSignedIntegerDataType( 2 ); // $ExpectType DataType
27+
}
28+
29+
// The compiler throws an error if the function is provided an unsupported number of arguments...
30+
{
31+
minSignedIntegerDataType(); // $ExpectError
32+
minSignedIntegerDataType( 3, 3 ); // $ExpectError
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
22+
var exp2 = require( '@stdlib/math/base/special/exp2' );
23+
var minSignedIntegerDataType = require( './../lib' );
24+
25+
// Generate random powers:
26+
var exp = discreteUniform( 100, 0, 40, {
27+
'dtype': 'generic'
28+
});
29+
30+
// Determine the minimum data type for each generated value...
31+
var v;
32+
var i;
33+
for ( i = 0; i < exp.length; i++ ) {
34+
v = exp2( exp[ i ] );
35+
console.log( 'min(%d) => %s', v, minSignedIntegerDataType( v ) );
36+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
/**
22+
* Determine the minimum ndarray data type for storing a provided signed integer value.
23+
*
24+
* @module @stdlib/ndarray/base/min-signed-integer-dtype
25+
*
26+
* @example
27+
* var minSignedIntegerDataType = require( '@stdlib/ndarray/base/min-signed-integer-dtype' );
28+
*
29+
* var dt = minSignedIntegerDataType( 1280 );
30+
* // returns 'int16'
31+
*
32+
* dt = minSignedIntegerDataType( 3 );
33+
* // returns 'int8'
34+
*/
35+
36+
// MODULES //
37+
38+
var minSignedIntegerDataType = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = minSignedIntegerDataType;

0 commit comments

Comments
 (0)