@@ -88,40 +88,33 @@ def round_float(
88
88
isignificand = math .floor (fsignificand )
89
89
delta = fsignificand - isignificand
90
90
91
+ code_is_odd = (
92
+ _isodd (isignificand )
93
+ if fi .precision > 1
94
+ else (isignificand != 0 and _isodd (expval + bias ))
95
+ )
96
+
91
97
# fmt: off
92
- if (
98
+ should_round_away = (
93
99
(rnd == RoundMode .TowardPositive and not sign and delta > 0 )
94
100
or (rnd == RoundMode .TowardNegative and sign and delta > 0 )
95
101
or (rnd == RoundMode .TiesToAway and delta >= 0.5 )
96
102
or (rnd == RoundMode .TiesToEven and delta > 0.5 )
97
- or (rnd == RoundMode .TiesToEven and delta == 0.5 and _isodd ( isignificand ) )
103
+ or (rnd == RoundMode .TiesToEven and delta == 0.5 and code_is_odd )
98
104
or (rnd == RoundMode .Stochastic and delta > (0.5 + srbits ) * 2.0 ** - srnumbits )
99
- ):
100
- isignificand += 1
101
-
102
- ## Special case for Precision=1, all-log format with zero.
103
- # The logic is simply duplicated (and isignificand overwritten) for clarity.
104
- if fi .precision == 1 :
105
- isignificand = math .floor (fsignificand )
106
- code_is_odd = isignificand != 0 and _isodd (expval + bias )
107
- if (
108
- (rnd == RoundMode .TowardPositive and not sign and delta > 0 )
109
- or (rnd == RoundMode .TowardNegative and sign and delta > 0 )
110
- or (rnd == RoundMode .TiesToAway and delta >= 0.5 )
111
- or (rnd == RoundMode .TiesToEven and delta > 0.5 )
112
- or (rnd == RoundMode .TiesToEven and delta == 0.5 and code_is_odd )
113
- or (rnd == RoundMode .Stochastic and delta > (0.5 + srbits ) * 2.0 ** - srnumbits )
114
- ):
115
- # Go to nextUp.
116
- # Increment isignificand if zero,
117
- # else increment exponent
105
+ )
106
+ # fmt: on
107
+
108
+ if should_round_away :
109
+ if fi .precision > 1 :
110
+ isignificand += 1
111
+ else :
112
+ # Increment isignificand if zero, else increment exponent
118
113
if isignificand == 0 :
119
114
isignificand = 1
120
115
else :
121
116
assert isignificand == 1
122
117
expval += 1
123
- ## End special case for Precision=1.
124
- # fmt: on
125
118
126
119
# Reconstruct rounded result to float
127
120
result = isignificand * (2.0 ** expval )
0 commit comments