@@ -72,40 +72,42 @@ def round_float(
72
72
expval = max (expval , 1 - bias )
73
73
74
74
# Lift to "integer * 2^e"
75
- expval -= t
75
+ expval = expval - p + 1
76
76
77
77
fsignificand = vpos * 2.0 ** - expval
78
78
79
79
# Round
80
80
isignificand = math .floor (fsignificand )
81
- if isignificand != fsignificand :
82
- # Need to round
83
- if rnd == RoundMode .TowardZero :
84
- pass
85
- elif rnd == RoundMode .TowardPositive :
86
- isignificand += 1 if not sign else 0
87
- elif rnd == RoundMode .TowardNegative :
88
- isignificand += 1 if sign else 0
89
- else :
90
- # Round to nearest
91
- d = fsignificand - isignificand
92
- if d > 0.5 :
93
- isignificand += 1
94
- elif d == 0.5 :
95
- # Tie
96
- if rnd == RoundMode .TiesToAway :
97
- isignificand += 1
98
- else :
99
- # All other modes tie to even
100
- if fi .precision == 1 :
101
- # No bits in significand
102
- assert (isignificand == 1 ) or (isignificand == 0 )
103
- biased_exp = expval + bias
104
- if _isodd (biased_exp ):
105
- expval += 1
106
- else :
107
- if _isodd (isignificand ):
108
- isignificand += 1
81
+ delta = fsignificand - isignificand
82
+ if (
83
+ (rnd == RoundMode .TowardPositive and not sign )
84
+ or (rnd == RoundMode .TowardNegative and sign )
85
+ or (rnd == RoundMode .TiesToAway and delta >= 0.5 )
86
+ or (rnd == RoundMode .TiesToEven and delta > 0.5 )
87
+ or (rnd == RoundMode .TiesToEven and delta == 0.5 and _isodd (isignificand ))
88
+ ):
89
+ isignificand += 1
90
+
91
+ ## Special case for Precision=1, all-log format with zero.
92
+ if fi .precision == 1 :
93
+ # The logic is simply duplicated for clarity of reading.
94
+ isignificand = math .floor (fsignificand )
95
+ code_is_odd = isignificand != 0 and _isodd (expval + bias )
96
+ if (
97
+ (rnd == RoundMode .TowardPositive and not sign )
98
+ or (rnd == RoundMode .TowardNegative and sign )
99
+ or (rnd == RoundMode .TiesToAway and delta >= 0.5 )
100
+ or (rnd == RoundMode .TiesToEven and delta > 0.5 )
101
+ or (rnd == RoundMode .TiesToEven and delta == 0.5 and code_is_odd )
102
+ ):
103
+ # Go to nextUp.
104
+ # Increment isignificand if zero,
105
+ # else increment exponent
106
+ if isignificand == 0 :
107
+ isignificand = 1
108
+ else :
109
+ assert isignificand == 1
110
+ expval += 1
109
111
110
112
result = isignificand * (2.0 ** expval )
111
113
0 commit comments