diff --git a/base/lib/index.js b/base/lib/index.js index faaa9a1d..b2fc3296 100644 --- a/base/lib/index.js +++ b/base/lib/index.js @@ -927,6 +927,24 @@ setReadOnly( ns, 'map4d', require( './../../base/map4d' ) ); */ setReadOnly( ns, 'map5d', require( './../../base/map5d' ) ); +/** +* @name minSignedIntegerDataType +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/array/base/min-signed-integer-dtype} +*/ +setReadOnly( ns, 'minSignedIntegerDataType', require( './../../base/min-signed-integer-dtype' ) ); + +/** +* @name minUnsignedIntegerDataType +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/array/base/min-unsigned-integer-dtype} +*/ +setReadOnly( ns, 'minUnsignedIntegerDataType', require( './../../base/min-unsigned-integer-dtype' ) ); + /** * @name mskbinary2d * @memberof ns diff --git a/base/min-signed-integer-dtype/README.md b/base/min-signed-integer-dtype/README.md new file mode 100644 index 00000000..4918899a --- /dev/null +++ b/base/min-signed-integer-dtype/README.md @@ -0,0 +1,132 @@ + + +# Minimum Data Type + +> Determine the minimum array [data type][@stdlib/array/dtypes] for storing a provided signed integer value. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var minSignedIntegerDataType = require( '@stdlib/array/base/min-signed-integer-dtype' ); +``` + +#### minSignedIntegerDataType( value ) + +Returns the minimum array [data type][@stdlib/array/dtypes] for storing a provided signed integer value. + +```javascript +var dt = minSignedIntegerDataType( 9999 ); +// returns 'int16' + +dt = minSignedIntegerDataType( -3 ); +// returns 'int8' + +dt = minSignedIntegerDataType( 3 ); +// returns 'int8' + +dt = minSignedIntegerDataType( 1e100 ); +// returns 'float64' +``` + +
+ + + + + +
+ +## Notes + +- Once a provided integer value exceeds the maximum values of all supported signed integer [data types][@stdlib/array/dtypes], the function defaults to returning `'float64'`. + +
+ + + + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var exp2 = require( '@stdlib/math/base/special/exp2' ); +var minSignedIntegerDataType = require( '@stdlib/array/base/min-signed-integer-dtype' ); + +// Generate random powers: +var exp = discreteUniform( 100, 0, 40, { + 'dtype': 'generic' +}); + +// Determine the minimum data type for each generated value... +var v; +var i; +for ( i = 0; i < exp.length; i++ ) { + v = exp2( exp[ i ] ); + console.log( 'min(%d) => %s', v, minSignedIntegerDataType( v ) ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/base/min-signed-integer-dtype/benchmark/benchmark.js b/base/min-signed-integer-dtype/benchmark/benchmark.js new file mode 100644 index 00000000..43982d07 --- /dev/null +++ b/base/min-signed-integer-dtype/benchmark/benchmark.js @@ -0,0 +1,64 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var minSignedIntegerDataType = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var N; + var i; + + values = [ + 0, + 1, + -128, + 128, + 99999, + -99999, + 1.0e100, + -1.0e100, + -1234567890, + 1234567890 + ]; + N = values.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = minSignedIntegerDataType( values[ i%N ] ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/base/min-signed-integer-dtype/docs/repl.txt b/base/min-signed-integer-dtype/docs/repl.txt new file mode 100644 index 00000000..1c463a4c --- /dev/null +++ b/base/min-signed-integer-dtype/docs/repl.txt @@ -0,0 +1,27 @@ + +{{alias}}( value ) + Returns the minimum array data type for storing a provided signed integer + value. + + Parameters + ---------- + value: number + Signed integer value. + + Returns + ------- + dt: string + Array data type. + + Examples + -------- + > var dt = {{alias}}( 3 ) + 'int8' + > dt = {{alias}}( -3 ) + 'int8' + > dt = {{alias}}( 1280 ) + 'int16' + + See Also + -------- + diff --git a/base/min-signed-integer-dtype/docs/types/index.d.ts b/base/min-signed-integer-dtype/docs/types/index.d.ts new file mode 100644 index 00000000..eb313ca7 --- /dev/null +++ b/base/min-signed-integer-dtype/docs/types/index.d.ts @@ -0,0 +1,49 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { SignedIntegerDataType } from '@stdlib/types/array'; + +/** +* Output data type. +*/ +type DataType = SignedIntegerDataType | 'float64'; + +/** +* Returns the minimum array data type for storing a provided signed integer value. +* +* @param value - scalar value +* @returns array data type +* +* @example +* var dt = minSignedIntegerDataType( 1280 ); +* // returns 'int16' +* +* @example +* var dt = minSignedIntegerDataType( 3 ); +* // returns 'int8' +*/ +declare function minSignedIntegerDataType( value: number ): DataType; + + +// EXPORTS // + +export = minSignedIntegerDataType; diff --git a/base/min-signed-integer-dtype/docs/types/test.ts b/base/min-signed-integer-dtype/docs/types/test.ts new file mode 100644 index 00000000..ba6e813e --- /dev/null +++ b/base/min-signed-integer-dtype/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import minSignedIntegerDataType = require( './index' ); + + +// TESTS // + +// The function returns a data type.. +{ + minSignedIntegerDataType( 2 ); // $ExpectType DataType +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + minSignedIntegerDataType(); // $ExpectError + minSignedIntegerDataType( 3, 3 ); // $ExpectError +} diff --git a/base/min-signed-integer-dtype/examples/index.js b/base/min-signed-integer-dtype/examples/index.js new file mode 100644 index 00000000..67c18fed --- /dev/null +++ b/base/min-signed-integer-dtype/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var exp2 = require( '@stdlib/math/base/special/exp2' ); +var minSignedIntegerDataType = require( './../lib' ); + +// Generate random powers: +var exp = discreteUniform( 100, 0, 40, { + 'dtype': 'generic' +}); + +// Determine the minimum data type for each generated value... +var v; +var i; +for ( i = 0; i < exp.length; i++ ) { + v = exp2( exp[ i ] ); + console.log( 'min(%d) => %s', v, minSignedIntegerDataType( v ) ); +} diff --git a/base/min-signed-integer-dtype/lib/index.js b/base/min-signed-integer-dtype/lib/index.js new file mode 100644 index 00000000..2ce66300 --- /dev/null +++ b/base/min-signed-integer-dtype/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Determine the minimum array data type for storing a provided signed integer value. +* +* @module @stdlib/array/base/min-signed-integer-dtype +* +* @example +* var minSignedIntegerDataType = require( '@stdlib/array/base/min-signed-integer-dtype' ); +* +* var dt = minSignedIntegerDataType( 1280 ); +* // returns 'int16' +* +* dt = minSignedIntegerDataType( 3 ); +* // returns 'int8' +*/ + +// MODULES // + +var minSignedIntegerDataType = require( './main.js' ); + + +// EXPORTS // + +module.exports = minSignedIntegerDataType; diff --git a/base/min-signed-integer-dtype/lib/main.js b/base/min-signed-integer-dtype/lib/main.js new file mode 100644 index 00000000..4710f6ed --- /dev/null +++ b/base/min-signed-integer-dtype/lib/main.js @@ -0,0 +1,75 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var INT8_MIN = require( '@stdlib/constants/int8/min' ); +var INT16_MIN = require( '@stdlib/constants/int16/min' ); +var INT32_MIN = require( '@stdlib/constants/int32/min' ); +var INT8_MAX = require( '@stdlib/constants/int8/max' ); +var INT16_MAX = require( '@stdlib/constants/int16/max' ); +var INT32_MAX = require( '@stdlib/constants/int32/max' ); + + +// MAIN // + +/** +* Returns the minimum array data type for storing a provided signed integer value. +* +* @param {integer} value - scalar value +* @returns {string} array data type +* +* @example +* var dt = minSignedIntegerDataType( 9999 ); +* // returns 'int16' +* +* @example +* var dt = minSignedIntegerDataType( 3 ); +* // returns 'int8' +*/ +function minSignedIntegerDataType( value ) { + if ( value < 0 ) { + if ( value >= INT8_MIN ) { + return 'int8'; + } + if ( value >= INT16_MIN ) { + return 'int16'; + } + if ( value >= INT32_MIN ) { + return 'int32'; + } + return 'float64'; + } + if ( value <= INT8_MAX ) { + return 'int8'; + } + if ( value <= INT16_MAX ) { + return 'int16'; + } + if ( value <= INT32_MAX ) { + return 'int32'; + } + return 'float64'; +} + + +// EXPORTS // + +module.exports = minSignedIntegerDataType; diff --git a/base/min-signed-integer-dtype/package.json b/base/min-signed-integer-dtype/package.json new file mode 100644 index 00000000..8e80d1c3 --- /dev/null +++ b/base/min-signed-integer-dtype/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/array/base/min-signed-integer-dtype", + "version": "0.0.0", + "description": "Determine the minimum array data type for storing a provided signed integer value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "array", + "base", + "scalar", + "type", + "memory", + "minimum", + "integer", + "int", + "signed", + "dtype", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/base/min-signed-integer-dtype/test/test.js b/base/min-signed-integer-dtype/test/test.js new file mode 100644 index 00000000..416bf880 --- /dev/null +++ b/base/min-signed-integer-dtype/test/test.js @@ -0,0 +1,75 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var minSignedIntegerDataType = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof minSignedIntegerDataType, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns the minimum array data type for storing a provided signed integer value', function test( t ) { + var expected; + var actual; + var values; + var i; + + values = [ + 0, + 1, + -1, + 127, + -128, + 300, // >2**8 + -300, + 65537, // >2**16 + -65537, + 99999999, + 4294967297, // >2**32 + -4294967297 + ]; + expected = [ + 'int8', + 'int8', + 'int8', + 'int8', + 'int8', + 'int16', + 'int16', + 'int32', + 'int32', + 'int32', + 'float64', + 'float64' + + ]; + for ( i = 0; i < values.length; i++ ) { + actual = minSignedIntegerDataType( values[i] ); + t.strictEqual( actual, expected[ i ], 'returns expected value when provided '+values[i] ); + } + t.end(); +}); diff --git a/base/min-unsigned-integer-dtype/README.md b/base/min-unsigned-integer-dtype/README.md new file mode 100644 index 00000000..665bed77 --- /dev/null +++ b/base/min-unsigned-integer-dtype/README.md @@ -0,0 +1,135 @@ + + +# Minimum Data Type + +> Determine the minimum array [data type][@stdlib/array/dtypes] for storing a provided unsigned integer value. + + + +
+ +
+ + + + + +
+ +## Usage + + + +```javascript +var minUnsignedIntegerDataType = require( '@stdlib/array/base/min-unsigned-integer-dtype' ); +``` + +#### minUnsignedIntegerDataType( value ) + +Returns the minimum array [data type][@stdlib/array/dtypes] for storing a provided unsigned integer value. + + + +```javascript +var dt = minUnsignedIntegerDataType( 9999 ); +// returns 'uint16' + +dt = minUnsignedIntegerDataType( 3 ); +// returns 'uint8' + +dt = minUnsignedIntegerDataType( 1e100 ); +// returns 'float64' +``` + +
+ + + + + +
+ +## Notes + +- 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'`. + +
+ + + + + +
+ +## Examples + + + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var exp2 = require( '@stdlib/math/base/special/exp2' ); +var minUnsignedIntegerDataType = require( '@stdlib/array/base/min-unsigned-integer-dtype' ); + +// Generate random powers: +var exp = discreteUniform( 100, 0, 40, { + 'dtype': 'generic' +}); + +// Determine the minimum data type for each generated value... +var v; +var i; +for ( i = 0; i < exp.length; i++ ) { + v = exp2( exp[ i ] ); + console.log( 'min(%d) => %s', v, minUnsignedIntegerDataType( v ) ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/base/min-unsigned-integer-dtype/benchmark/benchmark.js b/base/min-unsigned-integer-dtype/benchmark/benchmark.js new file mode 100644 index 00000000..018daad9 --- /dev/null +++ b/base/min-unsigned-integer-dtype/benchmark/benchmark.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var minUnsignedIntegerDataType = require( './../lib' ); // eslint-disable-line id-length + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var N; + var i; + + values = [ + 0, + 1, + 128, + 99999, + 1.0e100, + 1234567890 + ]; + N = values.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = minUnsignedIntegerDataType( values[ i%N ] ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/base/min-unsigned-integer-dtype/docs/repl.txt b/base/min-unsigned-integer-dtype/docs/repl.txt new file mode 100644 index 00000000..0d6ee3e6 --- /dev/null +++ b/base/min-unsigned-integer-dtype/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}( value ) + Returns the minimum array data type for storing a provided unsigned integer + value. + + Parameters + ---------- + value: number + Unsigned integer value. + + Returns + ------- + dt: string + Array data type. + + Examples + -------- + > var dt = {{alias}}( 3 ) + 'uint8' + > dt = {{alias}}( 1280 ) + 'uint16' + + See Also + -------- + diff --git a/base/min-unsigned-integer-dtype/docs/types/index.d.ts b/base/min-unsigned-integer-dtype/docs/types/index.d.ts new file mode 100644 index 00000000..5bee452d --- /dev/null +++ b/base/min-unsigned-integer-dtype/docs/types/index.d.ts @@ -0,0 +1,49 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { UnsignedIntegerDataType } from '@stdlib/types/array'; + +/** +* Output data type. +*/ +type DataType = UnsignedIntegerDataType | 'float64'; + +/** +* Returns the minimum array data type for storing a provided unsigned integer value. +* +* @param value - scalar value +* @returns array data type +* +* @example +* var dt = minUnsignedIntegerDataType( 1280 ); +* // returns 'uint16' +* +* @example +* var dt = minUnsignedIntegerDataType( 3 ); +* // returns 'uint8' +*/ +declare function minUnsignedIntegerDataType( value: number ): DataType; + + +// EXPORTS // + +export = minUnsignedIntegerDataType; diff --git a/base/min-unsigned-integer-dtype/docs/types/test.ts b/base/min-unsigned-integer-dtype/docs/types/test.ts new file mode 100644 index 00000000..a7335360 --- /dev/null +++ b/base/min-unsigned-integer-dtype/docs/types/test.ts @@ -0,0 +1,33 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import minUnsignedIntegerDataType = require( './index' ); + + +// TESTS // + +// The function returns a data type.. +{ + minUnsignedIntegerDataType( 2 ); // $ExpectType DataType +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + minUnsignedIntegerDataType(); // $ExpectError + minUnsignedIntegerDataType( 3, 3 ); // $ExpectError +} diff --git a/base/min-unsigned-integer-dtype/examples/index.js b/base/min-unsigned-integer-dtype/examples/index.js new file mode 100644 index 00000000..f20f66c8 --- /dev/null +++ b/base/min-unsigned-integer-dtype/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var exp2 = require( '@stdlib/math/base/special/exp2' ); +var minUnsignedIntegerDataType = require( './../lib' ); // eslint-disable-line id-length + +// Generate random powers: +var exp = discreteUniform( 100, 0, 40, { + 'dtype': 'generic' +}); + +// Determine the minimum data type for each generated value... +var v; +var i; +for ( i = 0; i < exp.length; i++ ) { + v = exp2( exp[ i ] ); + console.log( 'min(%d) => %s', v, minUnsignedIntegerDataType( v ) ); +} diff --git a/base/min-unsigned-integer-dtype/lib/index.js b/base/min-unsigned-integer-dtype/lib/index.js new file mode 100644 index 00000000..b3b8598d --- /dev/null +++ b/base/min-unsigned-integer-dtype/lib/index.js @@ -0,0 +1,43 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Determine the minimum array data type for storing a provided unsigned integer value. +* +* @module @stdlib/array/base/min-unsigned-integer-dtype +* +* @example +* var minUnsignedIntegerDataType = require( '@stdlib/array/base/min-unsigned-integer-dtype' ); +* +* var dt = minUnsignedIntegerDataType( 1280 ); +* // returns 'uint16' +* +* dt = minUnsignedIntegerDataType( 3 ); +* // returns 'uint8' +*/ + +// MODULES // + +var minUnsignedIntegerDataType = require( './main.js' ); // eslint-disable-line id-length + + +// EXPORTS // + +module.exports = minUnsignedIntegerDataType; diff --git a/base/min-unsigned-integer-dtype/lib/main.js b/base/min-unsigned-integer-dtype/lib/main.js new file mode 100644 index 00000000..9660f7c4 --- /dev/null +++ b/base/min-unsigned-integer-dtype/lib/main.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var UINT8_MAX = require( '@stdlib/constants/uint8/max' ); +var UINT16_MAX = require( '@stdlib/constants/uint16/max' ); +var UINT32_MAX = require( '@stdlib/constants/uint32/max' ); + + +// MAIN // + +/** +* Returns the minimum array data type for storing a provided unsigned integer value. +* +* @param {uinteger} value - scalar value +* @returns {string} array data type +* +* @example +* var dt = minUnsignedIntegerDataType( 9999 ); +* // returns 'uint16' +* +* @example +* var dt = minUnsignedIntegerDataType( 3 ); +* // returns 'uint8' +*/ +function minUnsignedIntegerDataType( value ) { // eslint-disable-line id-length + if ( value <= UINT8_MAX ) { + return 'uint8'; + } + if ( value <= UINT16_MAX ) { + return 'uint16'; + } + if ( value <= UINT32_MAX ) { + return 'uint32'; + } + return 'float64'; +} + + +// EXPORTS // + +module.exports = minUnsignedIntegerDataType; diff --git a/base/min-unsigned-integer-dtype/package.json b/base/min-unsigned-integer-dtype/package.json new file mode 100644 index 00000000..08490f62 --- /dev/null +++ b/base/min-unsigned-integer-dtype/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/array/base/min-unsigned-integer-dtype", + "version": "0.0.0", + "description": "Determine the minimum array data type for storing a provided unsigned integer value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "array", + "base", + "scalar", + "type", + "memory", + "minimum", + "integer", + "int", + "uint", + "unsigned", + "dtype", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/base/min-unsigned-integer-dtype/test/test.js b/base/min-unsigned-integer-dtype/test/test.js new file mode 100644 index 00000000..a2408a41 --- /dev/null +++ b/base/min-unsigned-integer-dtype/test/test.js @@ -0,0 +1,65 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var minUnsignedIntegerDataType = require( './../lib' ); // eslint-disable-line id-length + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof minUnsignedIntegerDataType, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns the minimum array data type for storing a provided unsigned integer value', function test( t ) { + var expected; + var actual; + var values; + var i; + + values = [ + 0, + 1, + 128, + 300, // >2**8 + 65537, // >2**16 + 99999999, + 4294967297 // >2**32 + ]; + expected = [ + 'uint8', + 'uint8', + 'uint8', + 'uint16', + 'uint32', + 'uint32', + 'float64' + + ]; + for ( i = 0; i < values.length; i++ ) { + actual = minUnsignedIntegerDataType( values[i] ); + t.strictEqual( actual, expected[ i ], 'returns expected value when provided '+values[i] ); + } + t.end(); +});