Skip to content

Commit c6ba6d1

Browse files
committed
Add support for complex number dtypes
1 parent 2cfd19d commit c6ba6d1

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

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

+48
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@
2222

2323
var isInteger = require( '@stdlib/math/base/assert/is-integer' );
2424
var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
25+
var isComplex64 = require( '@stdlib/assert/is-complex64' );
26+
var isComplex128 = require( '@stdlib/assert/is-complex128' );
27+
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
2528
var PINF = require( '@stdlib/constants/float64/pinf' );
2629
var NINF = require( '@stdlib/constants/float64/ninf' );
2730
var FLOAT32_SMALLEST_SUBNORMAL = require( '@stdlib/constants/float32/smallest-subnormal' ); // eslint-disable-line id-length
31+
var FLOAT32_MAX_SAFE_INTEGER = require( '@stdlib/constants/float32/max-safe-integer' );
32+
var FLOAT32_MIN_SAFE_INTEGER = require( '@stdlib/constants/float32/min-safe-integer' );
2833
var INT8_MIN = require( '@stdlib/constants/int8/min' );
2934
var INT16_MIN = require( '@stdlib/constants/int16/min' );
3035
var INT32_MIN = require( '@stdlib/constants/int32/min' );
@@ -33,6 +38,37 @@ var UINT16_MAX = require( '@stdlib/constants/uint16/max' );
3338
var UINT32_MAX = require( '@stdlib/constants/uint32/max' );
3439

3540

41+
// FUNCTIONS //
42+
43+
/**
44+
* Returns the minimum floating-point array data type of the closest "kind" necessary for storing a provided scalar.
45+
*
46+
* @private
47+
* @param {number} value - real value
48+
* @returns {string} array data type
49+
*/
50+
function minFloatDataType( value ) {
51+
if ( value !== value || value === PINF || value === NINF ) {
52+
return 'float32';
53+
}
54+
if ( isInteger( value ) ) {
55+
if ( value >= FLOAT32_MIN_SAFE_INTEGER && value <= FLOAT32_MAX_SAFE_INTEGER ) { // eslint-disable-line max-len
56+
return 'float32';
57+
}
58+
return 'float64';
59+
}
60+
// Assume that if we are provided a tiny value, we don't want to underflow to zero by storing as `float32`...
61+
if (
62+
value > -FLOAT32_SMALLEST_SUBNORMAL &&
63+
value < FLOAT32_SMALLEST_SUBNORMAL
64+
) {
65+
return 'float64';
66+
}
67+
// 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)...
68+
return 'float32';
69+
}
70+
71+
3672
// MAIN //
3773

3874
/**
@@ -51,6 +87,18 @@ var UINT32_MAX = require( '@stdlib/constants/uint32/max' );
5187
*/
5288
function minDataType( value ) {
5389
if ( typeof value !== 'number' ) {
90+
if ( isComplexLike( value ) ) {
91+
if ( isComplex64( value ) ) {
92+
return 'complex64';
93+
}
94+
if ( isComplex128( value ) ) {
95+
return 'complex128';
96+
}
97+
if ( minFloatDataType( value.re ) === 'float64' || minFloatDataType( value.im ) === 'float64' ) {
98+
return 'complex128';
99+
}
100+
return 'complex64';
101+
}
54102
return 'generic';
55103
}
56104
if ( value !== value || value === PINF || value === NINF ) {

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

+84-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
var tape = require( 'tape' );
2424
var PINF = require( '@stdlib/constants/float64/pinf' );
2525
var NINF = require( '@stdlib/constants/float64/ninf' );
26+
var Complex64 = require( '@stdlib/complex/float32' );
27+
var Complex128 = require( '@stdlib/complex/float64' );
2628
var minDataType = require( './../lib' );
2729

2830

@@ -65,7 +67,69 @@ tape( 'the function returns the minimum array data type of the closest "kind" ne
6567
{},
6668
true,
6769
false,
68-
[]
70+
[],
71+
new Complex64( 3.0, 5.0 ),
72+
new Complex128( 3.0, 5.0 ),
73+
{
74+
're': 3.0,
75+
'im': 5.0
76+
},
77+
{
78+
're': 4294967297,
79+
'im': 5.0
80+
},
81+
{
82+
're': 3.0,
83+
'im': 4294967297
84+
},
85+
{
86+
're': -4294967297,
87+
'im': 5.0
88+
},
89+
{
90+
're': 3.0,
91+
'im': -4294967297
92+
},
93+
{
94+
're': -1.0e-46,
95+
'im': 5.0
96+
},
97+
{
98+
're': 3.0,
99+
'im': -1.0e-46
100+
},
101+
{
102+
're': NaN,
103+
'im': 5.0
104+
},
105+
{
106+
're': 3.0,
107+
'im': NaN
108+
},
109+
{
110+
're': PINF,
111+
'im': 5.0
112+
},
113+
{
114+
're': 3.0,
115+
'im': PINF
116+
},
117+
{
118+
're': NINF,
119+
'im': 5.0
120+
},
121+
{
122+
're': 3.0,
123+
'im': NINF
124+
},
125+
{
126+
're': 3.14,
127+
'im': 5.0
128+
},
129+
{
130+
're': 3.0,
131+
'im': 3.14
132+
}
69133
];
70134
expected = [
71135
'float32',
@@ -92,7 +156,25 @@ tape( 'the function returns the minimum array data type of the closest "kind" ne
92156
'generic',
93157
'generic',
94158
'generic',
95-
'generic'
159+
'generic',
160+
'complex64',
161+
'complex128',
162+
'complex64',
163+
'complex128',
164+
'complex128',
165+
'complex128',
166+
'complex128',
167+
'complex128',
168+
'complex128',
169+
'complex64',
170+
'complex64',
171+
'complex64',
172+
'complex64',
173+
'complex64',
174+
'complex64',
175+
'complex64',
176+
'complex64'
177+
96178
];
97179
for ( i = 0; i < values.length; i++ ) {
98180
actual = minDataType( values[i] );

0 commit comments

Comments
 (0)