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