Skip to content

Commit 800db1b

Browse files
Erennn7kgrytegururaj1512
authored
feat: add float16 dtype support to array/min-dtype
PR-URL: #14018 Co-authored-by: Athan Reines <kgryte@gmail.com> Co-authored-by: gururaj1512 <gururajgurram1512@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Gururaj Gurram <gururajgurram1512@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Ref: #13193
1 parent a820b8a commit 800db1b

6 files changed

Lines changed: 146 additions & 45 deletions

File tree

‎lib/node_modules/@stdlib/array/min-dtype/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Returns the minimum array [data type][@stdlib/array/dtypes] of the closest "kind
4646

4747
```javascript
4848
var dt = minDataType( 3.141592653589793 );
49-
// returns 'float32'
49+
// returns 'float16'
5050

5151
dt = minDataType( -3 );
5252
// returns 'int8'

‎lib/node_modules/@stdlib/array/min-dtype/docs/repl.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Examples
2222
--------
2323
> var dt = {{alias}}( 3.141592653589793 )
24-
'float32'
24+
'float16'
2525
> dt = {{alias}}( 3 )
2626
'uint8'
2727
> dt = {{alias}}( -3 )

‎lib/node_modules/@stdlib/array/min-dtype/docs/types/index.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { ComplexLike } from '@stdlib/types/complex';
3535
*
3636
* @example
3737
* var dt = minDataType( 3.141592653589793 );
38-
* // returns 'float32'
38+
* // returns 'float16'
3939
*
4040
* @example
4141
* var dt = minDataType( 3 );

‎lib/node_modules/@stdlib/array/min-dtype/lib/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* var minDataType = require( '@stdlib/array/min-dtype' );
2828
*
2929
* var dt = minDataType( 3.141592653589793 );
30-
* // returns 'float32'
30+
* // returns 'float16'
3131
*
3232
* dt = minDataType( 3 );
3333
* // returns 'uint8'

‎lib/node_modules/@stdlib/array/min-dtype/lib/main.js‎

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
2727
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
2828
var PINF = require( '@stdlib/constants/float64/pinf' );
2929
var NINF = require( '@stdlib/constants/float64/ninf' );
30+
var FLOAT16_SMALLEST_SUBNORMAL = require( '@stdlib/constants/float16/smallest-subnormal' ); // eslint-disable-line id-length
31+
var FLOAT16_MAX_SAFE_INTEGER = require( '@stdlib/constants/float16/max-safe-integer' );
32+
var FLOAT16_MIN_SAFE_INTEGER = require( '@stdlib/constants/float16/min-safe-integer' );
33+
var FLOAT16_MAX = require( '@stdlib/constants/float16/max' );
3034
var FLOAT32_SMALLEST_SUBNORMAL = require( '@stdlib/constants/float32/smallest-subnormal' ); // eslint-disable-line id-length
3135
var FLOAT32_MAX_SAFE_INTEGER = require( '@stdlib/constants/float32/max-safe-integer' );
3236
var FLOAT32_MIN_SAFE_INTEGER = require( '@stdlib/constants/float32/min-safe-integer' );
@@ -49,20 +53,38 @@ var UINT32_MAX = require( '@stdlib/constants/uint32/max' );
4953
*/
5054
function minFloatDataType( value ) {
5155
if ( value !== value || value === PINF || value === NINF ) {
52-
return 'float32';
56+
return 'float16';
5357
}
5458
if ( isInteger( value ) ) {
55-
if ( value >= FLOAT32_MIN_SAFE_INTEGER && value <= FLOAT32_MAX_SAFE_INTEGER ) { // eslint-disable-line max-len
59+
if (
60+
value >= FLOAT16_MIN_SAFE_INTEGER &&
61+
value <= FLOAT16_MAX_SAFE_INTEGER
62+
) {
63+
return 'float16';
64+
}
65+
if (
66+
value >= FLOAT32_MIN_SAFE_INTEGER &&
67+
value <= FLOAT32_MAX_SAFE_INTEGER
68+
) {
5669
return 'float32';
5770
}
5871
return 'float64';
5972
}
60-
// Assume that if we are provided a tiny value, we don't want to underflow to zero by storing as `float32`...
73+
// Assume that if we are provided a tiny value, we don't want to underflow to zero by storing in a smaller precision...
6174
if (
62-
value > -FLOAT32_SMALLEST_SUBNORMAL &&
63-
value < FLOAT32_SMALLEST_SUBNORMAL
75+
value > -FLOAT16_SMALLEST_SUBNORMAL &&
76+
value < FLOAT16_SMALLEST_SUBNORMAL
6477
) {
65-
return 'float64';
78+
if (
79+
value > -FLOAT32_SMALLEST_SUBNORMAL &&
80+
value < FLOAT32_SMALLEST_SUBNORMAL
81+
) {
82+
return 'float64';
83+
}
84+
return 'float32';
85+
}
86+
if ( value >= -FLOAT16_MAX && value <= FLOAT16_MAX ) {
87+
return 'float16';
6688
}
6789
// 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)...
6890
return 'float32';
@@ -79,7 +101,7 @@ function minFloatDataType( value ) {
79101
*
80102
* @example
81103
* var dt = minDataType( 3.141592653589793 );
82-
* // returns 'float32'
104+
* // returns 'float16'
83105
*
84106
* @example
85107
* var dt = minDataType( 3 );
@@ -99,11 +121,11 @@ function minDataType( value ) {
99121
return 'generic';
100122
}
101123
if ( value !== value || value === PINF || value === NINF ) {
102-
return 'float32';
124+
return 'float16';
103125
}
104126
if ( isInteger( value ) ) {
105127
if ( value === 0 && isNegativeZero( value ) ) {
106-
return 'float32';
128+
return 'float16';
107129
}
108130
if ( value < 0 ) {
109131
if ( value >= INT8_MIN ) {
@@ -128,12 +150,21 @@ function minDataType( value ) {
128150
}
129151
return 'float64';
130152
}
131-
// Assume that if we are provided a tiny value, we don't want to underflow to zero by storing as `float32`...
153+
// Assume that if we are provided a tiny value, we don't want to underflow to zero by storing in a smaller precision...
132154
if (
133-
value > -FLOAT32_SMALLEST_SUBNORMAL &&
134-
value < FLOAT32_SMALLEST_SUBNORMAL
155+
value > -FLOAT16_SMALLEST_SUBNORMAL &&
156+
value < FLOAT16_SMALLEST_SUBNORMAL
135157
) {
136-
return 'float64';
158+
if (
159+
value > -FLOAT32_SMALLEST_SUBNORMAL &&
160+
value < FLOAT32_SMALLEST_SUBNORMAL
161+
) {
162+
return 'float64';
163+
}
164+
return 'float32';
165+
}
166+
if ( value >= -FLOAT16_MAX && value <= FLOAT16_MAX ) {
167+
return 'float16';
137168
}
138169
// 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)...
139170
return 'float32';

‎lib/node_modules/@stdlib/array/min-dtype/test/test.js‎

Lines changed: 98 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ tape( 'main export is a function', function test( t ) {
3636
t.end();
3737
});
3838

39-
tape( 'the function returns the minimum array data type of the closest "kind" necessary for storing a provided scalar value', function test( t ) {
39+
tape( 'the function returns the minimum array data type of the closest "kind" necessary for storing a provided real scalar value', function test( t ) {
4040
var expected;
4141
var actual;
4242
var values;
@@ -57,8 +57,14 @@ tape( 'the function returns the minimum array data type of the closest "kind" ne
5757
-4294967297,
5858
3.14,
5959
-3.14,
60+
70000.5,
61+
-70000.5,
62+
1.0e20, // >10**5
63+
-1.0e20,
6064
1.0e40, // >10**38
6165
-1.0e40,
66+
-1.0e-10, // <10**-7
67+
1.0e-10,
6268
-1.0e-46, // <10**-45
6369
1.0e-46,
6470
PINF,
@@ -67,7 +73,55 @@ tape( 'the function returns the minimum array data type of the closest "kind" ne
6773
{},
6874
true,
6975
false,
70-
[],
76+
[]
77+
];
78+
expected = [
79+
'float16',
80+
'uint8',
81+
'uint8',
82+
'float16',
83+
'uint8',
84+
'int8',
85+
'uint16',
86+
'int16',
87+
'uint32',
88+
'int32',
89+
'float64',
90+
'float64',
91+
'float16',
92+
'float16',
93+
'float32',
94+
'float32',
95+
'float64',
96+
'float64',
97+
'float64',
98+
'float64',
99+
'float32',
100+
'float32',
101+
'float64',
102+
'float64',
103+
'float16',
104+
'float16',
105+
'generic',
106+
'generic',
107+
'bool',
108+
'bool',
109+
'generic'
110+
];
111+
for ( i = 0; i < values.length; i++ ) {
112+
actual = minDataType( values[i] );
113+
t.strictEqual( actual, expected[ i ], 'returns expected value when provided '+values[i] );
114+
}
115+
t.end();
116+
});
117+
118+
tape( 'the function returns the minimum array data type of the closest "kind" necessary for storing a provided complex number or other value', function test( t ) {
119+
var expected;
120+
var actual;
121+
var values;
122+
var i;
123+
124+
values = [
71125
new Complex64( 3.0, 5.0 ),
72126
new Complex128( 3.0, 5.0 ),
73127
new Complex64( 1.0, 1.0 ),
@@ -94,6 +148,22 @@ tape( 'the function returns the minimum array data type of the closest "kind" ne
94148
new Complex128( 1.0e40, 1.0 ),
95149
new Complex128( 1.0, -1.0e40 ),
96150
new Complex128( 1.0, 1.0e40 ),
151+
new Complex128( -1.0e10, 1.0 ),
152+
new Complex128( 1.0e10, 1.0 ),
153+
new Complex128( 1.0, -1.0e10 ),
154+
new Complex128( 1.0, 1.0e10 ),
155+
new Complex128( -1.0e-10, 1.0 ),
156+
new Complex128( 1.0e-10, 1.0 ),
157+
new Complex128( 1.0, -1.0e-10 ),
158+
new Complex128( 1.0, 1.0e-10 ),
159+
new Complex128( -70000.5, 1.0 ),
160+
new Complex128( 70000.5, 1.0 ),
161+
new Complex128( 1.0, -70000.5 ),
162+
new Complex128( 1.0, 70000.5 ),
163+
new Complex128( -30000.0, 1.0 ),
164+
new Complex128( 30000.0, 1.0 ),
165+
new Complex128( 1.0, -30000.0 ),
166+
new Complex128( 1.0, 30000.0 ),
97167

98168
{
99169
're': 3.0,
@@ -154,34 +224,17 @@ tape( 'the function returns the minimum array data type of the closest "kind" ne
154224
{
155225
're': 3.0,
156226
'im': 3.14
227+
},
228+
{
229+
're': 70000.5,
230+
'im': 5.0
231+
},
232+
{
233+
're': 3.0,
234+
'im': 70000.5
157235
}
158236
];
159237
expected = [
160-
'float32',
161-
'uint8',
162-
'uint8',
163-
'float32',
164-
'uint8',
165-
'int8',
166-
'uint16',
167-
'int16',
168-
'uint32',
169-
'int32',
170-
'float64',
171-
'float64',
172-
'float32',
173-
'float32',
174-
'float64',
175-
'float64',
176-
'float64',
177-
'float64',
178-
'float32',
179-
'float32',
180-
'generic',
181-
'generic',
182-
'bool',
183-
'bool',
184-
'generic',
185238
'complex64',
186239
'complex64',
187240
'complex64',
@@ -208,6 +261,22 @@ tape( 'the function returns the minimum array data type of the closest "kind" ne
208261
'complex128',
209262
'complex128',
210263
'complex128',
264+
'complex128',
265+
'complex128',
266+
'complex128',
267+
'complex128',
268+
'complex64',
269+
'complex64',
270+
'complex64',
271+
'complex64',
272+
'complex64',
273+
'complex64',
274+
'complex64',
275+
'complex64',
276+
'complex64',
277+
'complex64',
278+
'complex64',
279+
'complex64',
211280

212281
'complex64',
213282
'complex128',
@@ -223,8 +292,9 @@ tape( 'the function returns the minimum array data type of the closest "kind" ne
223292
'complex64',
224293
'complex64',
225294
'complex64',
295+
'complex64',
296+
'complex64',
226297
'complex64'
227-
228298
];
229299
for ( i = 0; i < values.length; i++ ) {
230300
actual = minDataType( values[i] );

0 commit comments

Comments
 (0)