Skip to content

Commit 1ecf411

Browse files
committed
This reverts commit 61c3b494b36663a35efe7d0af18b3b9f28030a14.
1 parent ba39703 commit 1ecf411

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

src/gfloat/round.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,34 +73,40 @@ def round_float(
7373

7474
fsignificand = vpos * 2.0**-expval
7575

76-
# Round.
76+
# Round
7777
isignificand = math.floor(fsignificand)
7878
delta = fsignificand - isignificand
79-
80-
# Is the integer codepoint odd or even?
81-
if rnd == RoundMode.TiesToEven:
82-
code_is_odd = _isodd(isignificand)
83-
if fi.precision == 1: # When precision == 1, also check exponent.
84-
code_is_odd = isignificand != 0 and _isodd(expval + bias)
85-
86-
# Rounded value will be either isignificand or isignificand+1.
8779
if (
88-
(
89-
rnd == RoundMode.TiesToEven
90-
and (delta > 0.5 or delta == 0.5 and code_is_odd)
91-
)
92-
or (rnd == RoundMode.TiesToAway and delta >= 0.5)
93-
or (rnd == RoundMode.TowardPositive and not sign and delta > 0)
80+
(rnd == RoundMode.TowardPositive and not sign and delta > 0)
9481
or (rnd == RoundMode.TowardNegative and sign and delta > 0)
82+
or (rnd == RoundMode.TiesToAway and delta >= 0.5)
83+
or (rnd == RoundMode.TiesToEven and delta > 0.5)
84+
or (rnd == RoundMode.TiesToEven and delta == 0.5 and _isodd(isignificand))
9585
):
9686
isignificand += 1
97-
if fi.precision == 1 and isignificand == 2:
98-
# Special case for p = 1: carry overflowed significand to exponent.
99-
# (Not needed for p>1 as we are reconstructing, not encoding)
100-
isignificand = 1
101-
expval += 1
10287

103-
# Reconstruct from integer significand and exponent
88+
## Special case for Precision=1, all-log format with zero.
89+
if fi.precision == 1:
90+
# The logic is simply duplicated for clarity of reading.
91+
isignificand = math.floor(fsignificand)
92+
code_is_odd = isignificand != 0 and _isodd(expval + bias)
93+
if (
94+
(rnd == RoundMode.TowardPositive and not sign and delta > 0)
95+
or (rnd == RoundMode.TowardNegative and sign and delta > 0)
96+
or (rnd == RoundMode.TiesToAway and delta >= 0.5)
97+
or (rnd == RoundMode.TiesToEven and delta > 0.5)
98+
or (rnd == RoundMode.TiesToEven and delta == 0.5 and code_is_odd)
99+
):
100+
# Go to nextUp.
101+
# Increment isignificand if zero,
102+
# else increment exponent
103+
if isignificand == 0:
104+
isignificand = 1
105+
else:
106+
assert isignificand == 1
107+
expval += 1
108+
## End special case for Precision=1.
109+
104110
result = isignificand * (2.0**expval)
105111

106112
if result == 0:

0 commit comments

Comments
 (0)