From 5b1419d6d80ca77594fe8af1e8a2f1a65be15011 Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Sun, 15 Dec 2024 03:24:03 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 50 +++++++ base/lib/index.js | 45 ++++++ base/min-signed-integer-dtype/README.md | 132 +++++++++++++++++ .../benchmark/benchmark.js | 64 +++++++++ base/min-signed-integer-dtype/docs/repl.txt | 27 ++++ .../docs/types/index.d.ts | 49 +++++++ .../docs/types/test.ts | 33 +++++ .../examples/index.js | 36 +++++ base/min-signed-integer-dtype/lib/index.js | 43 ++++++ base/min-signed-integer-dtype/lib/main.js | 75 ++++++++++ base/min-signed-integer-dtype/package.json | 71 +++++++++ base/min-signed-integer-dtype/test/test.js | 75 ++++++++++ base/min-unsigned-integer-dtype/README.md | 135 ++++++++++++++++++ .../benchmark/benchmark.js | 60 ++++++++ base/min-unsigned-integer-dtype/docs/repl.txt | 25 ++++ .../docs/types/index.d.ts | 49 +++++++ .../docs/types/test.ts | 33 +++++ .../examples/index.js | 36 +++++ base/min-unsigned-integer-dtype/lib/index.js | 43 ++++++ base/min-unsigned-integer-dtype/lib/main.js | 60 ++++++++ base/min-unsigned-integer-dtype/package.json | 72 ++++++++++ base/min-unsigned-integer-dtype/test/test.js | 65 +++++++++ 22 files changed, 1278 insertions(+) create mode 100644 base/min-signed-integer-dtype/README.md create mode 100644 base/min-signed-integer-dtype/benchmark/benchmark.js create mode 100644 base/min-signed-integer-dtype/docs/repl.txt create mode 100644 base/min-signed-integer-dtype/docs/types/index.d.ts create mode 100644 base/min-signed-integer-dtype/docs/types/test.ts create mode 100644 base/min-signed-integer-dtype/examples/index.js create mode 100644 base/min-signed-integer-dtype/lib/index.js create mode 100644 base/min-signed-integer-dtype/lib/main.js create mode 100644 base/min-signed-integer-dtype/package.json create mode 100644 base/min-signed-integer-dtype/test/test.js create mode 100644 base/min-unsigned-integer-dtype/README.md create mode 100644 base/min-unsigned-integer-dtype/benchmark/benchmark.js create mode 100644 base/min-unsigned-integer-dtype/docs/repl.txt create mode 100644 base/min-unsigned-integer-dtype/docs/types/index.d.ts create mode 100644 base/min-unsigned-integer-dtype/docs/types/test.ts create mode 100644 base/min-unsigned-integer-dtype/examples/index.js create mode 100644 base/min-unsigned-integer-dtype/lib/index.js create mode 100644 base/min-unsigned-integer-dtype/lib/main.js create mode 100644 base/min-unsigned-integer-dtype/package.json create mode 100644 base/min-unsigned-integer-dtype/test/test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e3211d3..45d106da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,8 @@ ##### Features +- [`14427c7`](https://github.com/stdlib-js/stdlib/commit/14427c79bc62f82b16cbadc9d34749901e48d105) - add `fill`, `map`, and `toReversed` to namespace +- [`a0d6619`](https://github.com/stdlib-js/stdlib/commit/a0d66193409576538d0f16aa89cbaeedec7898be) - add `minSignedIntegerDataType` and `minUnsignedIntegerDataType` to namespace - [`8b1548f`](https://github.com/stdlib-js/stdlib/commit/8b1548fb45c1ff131f5edac20cb984344a2d28ec) - update namespace TypeScript declarations [(#3190)](https://github.com/stdlib-js/stdlib/pull/3190) @@ -137,6 +139,50 @@ +
+ +#### [@stdlib/ndarray/base/min-signed-integer-dtype](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/min-signed-integer-dtype) + +
+ +
+ +##### Features + +- [`c3bffe5`](https://github.com/stdlib-js/stdlib/commit/c3bffe513318480e2ce4645bb05895df8148ee1e) - add `ndarray/base/min-signed-integer-dtype` + +
+ + + +
+ +
+ + + +
+ +#### [@stdlib/ndarray/base/min-unsigned-integer-dtype](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/min-unsigned-integer-dtype) + +
+ +
+ +##### Features + +- [`97e8c1a`](https://github.com/stdlib-js/stdlib/commit/97e8c1aff4f7c1a3b0946d15354a926d82e2120a) - add `ndarray/base/min-unsigned-integer-dtype` + +
+ + + +
+ +
+ + +
#### [@stdlib/ndarray/filter](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/filter) @@ -317,6 +363,10 @@ A total of 3 people contributed to this release. Thank you to the following cont
+- [`14427c7`](https://github.com/stdlib-js/stdlib/commit/14427c79bc62f82b16cbadc9d34749901e48d105) - **feat:** add `fill`, `map`, and `toReversed` to namespace _(by Athan Reines)_ +- [`a0d6619`](https://github.com/stdlib-js/stdlib/commit/a0d66193409576538d0f16aa89cbaeedec7898be) - **feat:** add `minSignedIntegerDataType` and `minUnsignedIntegerDataType` to namespace _(by Athan Reines)_ +- [`97e8c1a`](https://github.com/stdlib-js/stdlib/commit/97e8c1aff4f7c1a3b0946d15354a926d82e2120a) - **feat:** add `ndarray/base/min-unsigned-integer-dtype` _(by Athan Reines)_ +- [`c3bffe5`](https://github.com/stdlib-js/stdlib/commit/c3bffe513318480e2ce4645bb05895df8148ee1e) - **feat:** add `ndarray/base/min-signed-integer-dtype` _(by Athan Reines)_ - [`0d6bf75`](https://github.com/stdlib-js/stdlib/commit/0d6bf755cd3fcefbdf4751bc1f8e011bedefc057) - **refactor:** resolve error constructor and add todos _(by Athan Reines)_ - [`dbfd8f5`](https://github.com/stdlib-js/stdlib/commit/dbfd8f5c81d11be2142ebfc4f2f0bb0316ba7478) - **feat:** add `filterMap` to namespace _(by Athan Reines)_ - [`6ff153f`](https://github.com/stdlib-js/stdlib/commit/6ff153f9023cffac527b3243489e6413e989e940) - **feat:** add `ndarray/filter-map` _(by Athan Reines)_ diff --git a/base/lib/index.js b/base/lib/index.js index ac8a7816..c83c55a3 100644 --- a/base/lib/index.js +++ b/base/lib/index.js @@ -310,6 +310,15 @@ setReadOnly( ns, 'emptyLike', require( './../../base/empty-like' ) ); */ setReadOnly( ns, 'expandDimensions', require( './../../base/expand-dimensions' ) ); +/** +* @name fill +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/ndarray/base/fill} +*/ +setReadOnly( ns, 'fill', require( './../../base/fill' ) ); + /** * @name flag * @memberof ns @@ -391,6 +400,15 @@ setReadOnly( ns, 'ind2sub', require( './../../base/ind2sub' ) ); */ setReadOnly( ns, 'iterationOrder', require( './../../base/iteration-order' ) ); +/** +* @name map +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/ndarray/base/map} +*/ +setReadOnly( ns, 'map', require( './../../base/map' ) ); + /** * @name maxViewBufferIndex * @memberof ns @@ -427,6 +445,24 @@ setReadOnly( ns, 'maybeBroadcastArrays', require( './../../base/maybe-broadcast- */ setReadOnly( ns, 'metaDataProps', require( './../../base/meta-data-props' ) ); +/** +* @name minSignedIntegerDataType +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/ndarray/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/ndarray/base/min-unsigned-integer-dtype} +*/ +setReadOnly( ns, 'minUnsignedIntegerDataType', require( './../../base/min-unsigned-integer-dtype' ) ); + /** * @name minViewBufferIndex * @memberof ns @@ -787,6 +823,15 @@ setReadOnly( ns, 'sub2ind', require( './../../base/sub2ind' ) ); */ setReadOnly( ns, 'ndarray2array', require( './../../base/to-array' ) ); +/** +* @name toReversed +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/ndarray/base/to-reversed} +*/ +setReadOnly( ns, 'toReversed', require( './../../base/to-reversed' ) ); + /** * @name transpose * @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..9c73368b --- /dev/null +++ b/base/min-signed-integer-dtype/README.md @@ -0,0 +1,132 @@ + + +# Minimum Data Type + +> Determine the minimum ndarray [data type][@stdlib/ndarray/dtypes] for storing a provided signed integer value. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var minSignedIntegerDataType = require( '@stdlib/ndarray/base/min-signed-integer-dtype' ); +``` + +#### minSignedIntegerDataType( value ) + +Returns the minimum ndarray [data type][@stdlib/ndarray/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/ndarray/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/ndarray/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..b6f51b0c --- /dev/null +++ b/base/min-signed-integer-dtype/docs/repl.txt @@ -0,0 +1,27 @@ + +{{alias}}( value ) + Returns the minimum ndarray data type for storing a provided signed integer + value. + + Parameters + ---------- + value: number + Signed integer value. + + Returns + ------- + dt: string + ndarray 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..8e60afd5 --- /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/ndarray'; + +/** +* Output data type. +*/ +type DataType = SignedIntegerDataType | 'float64'; + +/** +* Returns the minimum ndarray data type for storing a provided signed integer value. +* +* @param value - scalar value +* @returns ndarray 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..dd9efd98 --- /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 ndarray data type for storing a provided signed integer value. +* +* @module @stdlib/ndarray/base/min-signed-integer-dtype +* +* @example +* var minSignedIntegerDataType = require( '@stdlib/ndarray/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..6a5c532d --- /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 ndarray data type for storing a provided signed integer value. +* +* @param {integer} value - scalar value +* @returns {string} ndarray 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..fcfaeae8 --- /dev/null +++ b/base/min-signed-integer-dtype/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/ndarray/base/min-signed-integer-dtype", + "version": "0.0.0", + "description": "Determine the minimum ndarray 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", + "ndarray", + "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..a17d9f9c --- /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 ndarray 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..628f1e75 --- /dev/null +++ b/base/min-unsigned-integer-dtype/README.md @@ -0,0 +1,135 @@ + + +# Minimum Data Type + +> Determine the minimum ndarray [data type][@stdlib/ndarray/dtypes] for storing a provided unsigned integer value. + + + +
+ +
+ + + + + +
+ +## Usage + + + +```javascript +var minUnsignedIntegerDataType = require( '@stdlib/ndarray/base/min-unsigned-integer-dtype' ); +``` + +#### minUnsignedIntegerDataType( value ) + +Returns the minimum ndarray [data type][@stdlib/ndarray/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/ndarray/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/ndarray/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..69f49135 --- /dev/null +++ b/base/min-unsigned-integer-dtype/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}( value ) + Returns the minimum ndarray data type for storing a provided unsigned + integer value. + + Parameters + ---------- + value: number + Unsigned integer value. + + Returns + ------- + dt: string + ndarray 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..c442159c --- /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/ndarray'; + +/** +* Output data type. +*/ +type DataType = UnsignedIntegerDataType | 'float64'; + +/** +* Returns the minimum ndarray data type for storing a provided unsigned integer value. +* +* @param value - scalar value +* @returns ndarray 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..882700fb --- /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 ndarray data type for storing a provided unsigned integer value. +* +* @module @stdlib/ndarray/base/min-unsigned-integer-dtype +* +* @example +* var minUnsignedIntegerDataType = require( '@stdlib/ndarray/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..3df0eaf6 --- /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 ndarray data type for storing a provided unsigned integer value. +* +* @param {uinteger} value - scalar value +* @returns {string} ndarray 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..69497186 --- /dev/null +++ b/base/min-unsigned-integer-dtype/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/ndarray/base/min-unsigned-integer-dtype", + "version": "0.0.0", + "description": "Determine the minimum ndarray 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", + "ndarray", + "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..6370599c --- /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 ndarray 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(); +});