Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/array/min-dtype/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Returns the minimum array [data type][@stdlib/array/dtypes] of the closest "kind

```javascript
var dt = minDataType( 3.141592653589793 );
// returns 'float32'
// returns 'float16'

dt = minDataType( -3 );
// returns 'int8'
Expand Down
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/array/min-dtype/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Examples
--------
> var dt = {{alias}}( 3.141592653589793 )
'float32'
'float16'
> dt = {{alias}}( 3 )
'uint8'
> dt = {{alias}}( -3 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @example
* var dt = minDataType( 3.141592653589793 );
* // returns 'float32'
* // returns 'float16'
*
* @example
* var dt = minDataType( 3 );
Expand Down Expand Up @@ -89,7 +89,7 @@
* var dt = minDataType( 'beep' );
* // returns 'generic'
*/
declare function minDataType( value: any ): 'generic';

Check warning on line 92 in lib/node_modules/@stdlib/array/min-dtype/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //
Expand Down
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/array/min-dtype/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* var minDataType = require( '@stdlib/array/min-dtype' );
*
* var dt = minDataType( 3.141592653589793 );
* // returns 'float32'
* // returns 'float16'
*
* dt = minDataType( 3 );
* // returns 'uint8'
Expand Down
61 changes: 46 additions & 15 deletions lib/node_modules/@stdlib/array/min-dtype/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var FLOAT16_SMALLEST_SUBNORMAL = require( '@stdlib/constants/float16/smallest-subnormal' ); // eslint-disable-line id-length
var FLOAT16_MAX_SAFE_INTEGER = require( '@stdlib/constants/float16/max-safe-integer' );
var FLOAT16_MIN_SAFE_INTEGER = require( '@stdlib/constants/float16/min-safe-integer' );
var FLOAT16_MAX = require( '@stdlib/constants/float16/max' );
var FLOAT32_SMALLEST_SUBNORMAL = require( '@stdlib/constants/float32/smallest-subnormal' ); // eslint-disable-line id-length
var FLOAT32_MAX_SAFE_INTEGER = require( '@stdlib/constants/float32/max-safe-integer' );
var FLOAT32_MIN_SAFE_INTEGER = require( '@stdlib/constants/float32/min-safe-integer' );
Expand All @@ -49,22 +53,40 @@ var UINT32_MAX = require( '@stdlib/constants/uint32/max' );
*/
function minFloatDataType( value ) {
if ( value !== value || value === PINF || value === NINF ) {
return 'float32';
return 'float16';
}
if ( isInteger( value ) ) {
if ( value >= FLOAT32_MIN_SAFE_INTEGER && value <= FLOAT32_MAX_SAFE_INTEGER ) { // eslint-disable-line max-len
if (
value >= FLOAT16_MIN_SAFE_INTEGER &&
value <= FLOAT16_MAX_SAFE_INTEGER
) {
return 'float16';
}
if (
value >= FLOAT32_MIN_SAFE_INTEGER &&
value <= FLOAT32_MAX_SAFE_INTEGER
) {
return 'float32';
}
return 'float64';
}
// Assume that if we are provided a tiny value, we don't want to underflow to zero by storing as `float32`...
// Assume that if we are provided a tiny value, we don't want to underflow to zero by storing as `float16`...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Assume that if we are provided a tiny value, we don't want to underflow to zero by storing as `float16`...
// Assume that if we are provided a tiny value, we don't want to underflow to zero by storing in a smaller precision...

if (
value > -FLOAT32_SMALLEST_SUBNORMAL &&
value < FLOAT32_SMALLEST_SUBNORMAL
value > -FLOAT16_SMALLEST_SUBNORMAL &&
value < FLOAT16_SMALLEST_SUBNORMAL
) {
return 'float64';
if (
value > -FLOAT32_SMALLEST_SUBNORMAL &&
value < FLOAT32_SMALLEST_SUBNORMAL
) {
return 'float64';
}
return 'float32';
}
// Any number which reaches this point is checked against the maximum half-precision floating-point number, as floating-point format supports a limited number of decimals (e.g., (1.0+EPS)*10**15 => 1000000000000000.2, which is less than ~65504)...
if ( value >= -FLOAT16_MAX && value <= FLOAT16_MAX ) {
return 'float16';
}
// Any number which reaches this point is less than the maximum single-precision floating-point number, as floating-point format supports a limited number of decimals (e.g., (1.0+EPS)*10**15 => 1000000000000000.2, which is less than ~3.4e38)...
return 'float32';
}

Expand All @@ -79,7 +101,7 @@ function minFloatDataType( value ) {
*
* @example
* var dt = minDataType( 3.141592653589793 );
* // returns 'float32'
* // returns 'float16'
*
* @example
* var dt = minDataType( 3 );
Expand All @@ -99,11 +121,11 @@ function minDataType( value ) {
return 'generic';
}
if ( value !== value || value === PINF || value === NINF ) {
return 'float32';
return 'float16';
}
if ( isInteger( value ) ) {
if ( value === 0 && isNegativeZero( value ) ) {
return 'float32';
return 'float16';
}
if ( value < 0 ) {
if ( value >= INT8_MIN ) {
Expand All @@ -128,14 +150,23 @@ function minDataType( value ) {
}
return 'float64';
}
// Assume that if we are provided a tiny value, we don't want to underflow to zero by storing as `float32`...
// Assume that if we are provided a tiny value, we don't want to underflow to zero by storing as `float16`...
if (
value > -FLOAT32_SMALLEST_SUBNORMAL &&
value < FLOAT32_SMALLEST_SUBNORMAL
value > -FLOAT16_SMALLEST_SUBNORMAL &&
value < FLOAT16_SMALLEST_SUBNORMAL
) {
return 'float64';
if (
value > -FLOAT32_SMALLEST_SUBNORMAL &&
value < FLOAT32_SMALLEST_SUBNORMAL
) {
return 'float64';
}
return 'float32';
}
// Any number which reaches this point is checked against the maximum half-precision floating-point number, as floating-point format supports a limited number of decimals (e.g., (1.0+EPS)*10**15 => 1000000000000000.2, which is less than ~65504)...
if ( value >= -FLOAT16_MAX && value <= FLOAT16_MAX ) {
return 'float16';
}
// Any number which reaches this point is less than the maximum single-precision floating-point number, as floating-point format supports a limited number of decimals (e.g., (1.0+EPS)*10**15 => 1000000000000000.2, which is less than ~3.4e38)...
return 'float32';
}

Expand Down
3 changes: 3 additions & 0 deletions lib/node_modules/@stdlib/array/min-dtype/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
"memory",
"minimum",
"dtype",
"float16",
"half",
"half-precision",
"utilities",
"utility",
"utils",
Expand Down
12 changes: 6 additions & 6 deletions lib/node_modules/@stdlib/array/min-dtype/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ tape( 'the function returns the minimum array data type of the closest "kind" ne
}
];
expected = [
'float32',
'float16',
'uint8',
'uint8',
'float32',
'float16',
'uint8',
'int8',
'uint16',
Expand All @@ -169,14 +169,14 @@ tape( 'the function returns the minimum array data type of the closest "kind" ne
'int32',
'float64',
'float64',
'float32',
'float32',
'float16',
'float16',
'float64',
'float64',
'float64',
'float64',
'float32',
'float32',
'float16',
'float16',
'generic',
'generic',
'bool',
Expand Down