Skip to content

Commit 3f40048

Browse files
committed
feat: add array/base/min-unsigned-integer-dtype
1 parent 80f51e6 commit 3f40048

File tree

10 files changed

+578
-0
lines changed

10 files changed

+578
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 array [data type][@stdlib/array/dtypes] for storing a provided unsigned 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+
<!-- eslint-disable id-length -->
40+
41+
```javascript
42+
var minUnsignedIntegerDataType = require( '@stdlib/array/base/min-unsigned-integer-dtype' );
43+
```
44+
45+
#### minUnsignedIntegerDataType( value )
46+
47+
Returns the minimum array [data type][@stdlib/array/dtypes] for storing a provided unsigned integer value.
48+
49+
<!-- eslint-disable id-length -->
50+
51+
```javascript
52+
var dt = minUnsignedIntegerDataType( 9999 );
53+
// returns 'uint16'
54+
55+
dt = minUnsignedIntegerDataType( 3 );
56+
// returns 'uint8'
57+
58+
dt = minUnsignedIntegerDataType( 1e100 );
59+
// returns 'float64'
60+
```
61+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
67+
68+
<section class="notes">
69+
70+
## Notes
71+
72+
- Once a provided integer value exceeds the maximum values of all supported unsigned integer [data types][@stdlib/array/dtypes], the function defaults to returning `'float64'`.
73+
74+
</section>
75+
76+
<!-- /.notes -->
77+
78+
<!-- Package usage examples. -->
79+
80+
<section class="examples">
81+
82+
## Examples
83+
84+
<!-- eslint no-undef: "error" -->
85+
86+
<!-- eslint-disable id-length -->
87+
88+
```javascript
89+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
90+
var exp2 = require( '@stdlib/math/base/special/exp2' );
91+
var minUnsignedIntegerDataType = require( '@stdlib/array/base/min-unsigned-integer-dtype' );
92+
93+
// Generate random powers:
94+
var exp = discreteUniform( 100, 0, 40, {
95+
'dtype': 'generic'
96+
});
97+
98+
// Determine the minimum data type for each generated value...
99+
var v;
100+
var i;
101+
for ( i = 0; i < exp.length; i++ ) {
102+
v = exp2( exp[ i ] );
103+
console.log( 'min(%d) => %s', v, minUnsignedIntegerDataType( v ) );
104+
}
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- 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. -->
112+
113+
<section class="references">
114+
115+
</section>
116+
117+
<!-- /.references -->
118+
119+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
120+
121+
<section class="related">
122+
123+
</section>
124+
125+
<!-- /.related -->
126+
127+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
128+
129+
<section class="links">
130+
131+
[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes
132+
133+
</section>
134+
135+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 minUnsignedIntegerDataType = require( './../lib' ); // eslint-disable-line id-length
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+
99999,
42+
1.0e100,
43+
1234567890
44+
];
45+
N = values.length;
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
out = minUnsignedIntegerDataType( values[ i%N ] );
50+
if ( typeof out !== 'string' ) {
51+
b.fail( 'should return a string' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isString( out ) ) {
56+
b.fail( 'should return a string' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( value )
3+
Returns the minimum array data type for storing a provided unsigned integer
4+
value.
5+
6+
Parameters
7+
----------
8+
value: number
9+
Unsigned integer value.
10+
11+
Returns
12+
-------
13+
dt: string
14+
Array data type.
15+
16+
Examples
17+
--------
18+
> var dt = {{alias}}( 3 )
19+
'uint8'
20+
> dt = {{alias}}( 1280 )
21+
'uint16'
22+
23+
See Also
24+
--------
25+
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 { UnsignedIntegerDataType } from '@stdlib/types/array';
24+
25+
/**
26+
* Output data type.
27+
*/
28+
type DataType = UnsignedIntegerDataType | 'float64';
29+
30+
/**
31+
* Returns the minimum array data type for storing a provided unsigned integer value.
32+
*
33+
* @param value - scalar value
34+
* @returns array data type
35+
*
36+
* @example
37+
* var dt = minUnsignedIntegerDataType( 1280 );
38+
* // returns 'uint16'
39+
*
40+
* @example
41+
* var dt = minUnsignedIntegerDataType( 3 );
42+
* // returns 'uint8'
43+
*/
44+
declare function minUnsignedIntegerDataType( value: number ): DataType;
45+
46+
47+
// EXPORTS //
48+
49+
export = minUnsignedIntegerDataType;
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 minUnsignedIntegerDataType = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a data type..
25+
{
26+
minUnsignedIntegerDataType( 2 ); // $ExpectType DataType
27+
}
28+
29+
// The compiler throws an error if the function is provided an unsupported number of arguments...
30+
{
31+
minUnsignedIntegerDataType(); // $ExpectError
32+
minUnsignedIntegerDataType( 3, 3 ); // $ExpectError
33+
}
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 minUnsignedIntegerDataType = require( './../lib' ); // eslint-disable-line id-length
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, minUnsignedIntegerDataType( v ) );
36+
}
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 array data type for storing a provided unsigned integer value.
23+
*
24+
* @module @stdlib/array/base/min-unsigned-integer-dtype
25+
*
26+
* @example
27+
* var minUnsignedIntegerDataType = require( '@stdlib/array/base/min-unsigned-integer-dtype' );
28+
*
29+
* var dt = minUnsignedIntegerDataType( 1280 );
30+
* // returns 'uint16'
31+
*
32+
* dt = minUnsignedIntegerDataType( 3 );
33+
* // returns 'uint8'
34+
*/
35+
36+
// MODULES //
37+
38+
var minUnsignedIntegerDataType = require( './main.js' ); // eslint-disable-line id-length
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = minUnsignedIntegerDataType;

0 commit comments

Comments
 (0)