@@ -48,18 +48,14 @@ def decode_ndarray(
48
48
49
49
expBias = fi .expBias
50
50
51
- iszero = (exp == 0 ) & (significand == 0 ) & fi .has_zero
52
- issubnormal = (exp == 0 ) & (significand != 0 ) & fi .has_subnormals
53
- isnormal = ~ iszero & ~ issubnormal
54
- expval = np .where (~ isnormal , 1 - expBias , exp - expBias )
55
- fsignificand = np .where (~ isnormal , significand * 2 ** - t , 1.0 + significand * 2 ** - t )
56
-
57
- # Normal/Subnormal/Zero case, other values will be overwritten
58
- fval = np .where (iszero , 0.0 , sign * fsignificand * 2.0 ** expval )
51
+ fval = np .zeros_like (codes , dtype = np .float64 )
52
+ isspecial = np .zeros_like (codes , dtype = bool )
59
53
60
54
if fi .has_infs :
61
55
fval = np .where (codes == fi .code_of_posinf , np .inf , fval )
56
+ isspecial |= codes == fi .code_of_posinf
62
57
fval = np .where (codes == fi .code_of_neginf , - np .inf , fval )
58
+ isspecial |= codes == fi .code_of_neginf
63
59
64
60
if fi .num_nans > 0 :
65
61
code_is_nan = codes == fi .code_of_nan
@@ -70,9 +66,21 @@ def decode_ndarray(
70
66
code_is_nan |= abse & (significand >= min_code_with_nan )
71
67
72
68
fval = np .where (code_is_nan , np .nan , fval )
69
+ isspecial |= code_is_nan
73
70
74
- # Negative zero
71
+ # Zero
72
+ iszero = ~ isspecial & (exp == 0 ) & (significand == 0 ) & fi .has_zero
73
+ fval = np .where (iszero , 0.0 , fval )
75
74
if fi .has_nz :
76
75
fval = np .where (iszero & (sign < 0 ), - 0.0 , fval )
77
76
77
+ issubnormal = (exp == 0 ) & (significand != 0 ) & fi .has_subnormals
78
+ expval = np .where (issubnormal , 1 - expBias , exp - expBias )
79
+ fsignificand = np .where (issubnormal , 0.0 , 1.0 ) + np .ldexp (significand , - t )
80
+
81
+ # Normal/Subnormal/Zero case, other values will be overwritten
82
+ expval_safe = np .where (isspecial | iszero , 0 , expval )
83
+ fval_finite_safe = sign * np .ldexp (fsignificand , expval_safe )
84
+ fval = np .where (~ (iszero | isspecial ), fval_finite_safe , fval )
85
+
78
86
return fval
0 commit comments