Skip to content

Commit 2f9273e

Browse files
committed
Tidier rounding logic
1 parent 7998d08 commit 2f9273e

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/gfloat/round.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,18 @@ def round_float(
9494
else (isignificand != 0 and _isodd(expval + bias))
9595
)
9696

97-
# fmt: off
98-
should_round_away = (
99-
(rnd == RoundMode.TowardPositive and not sign and delta > 0)
100-
or (rnd == RoundMode.TowardNegative and sign and delta > 0)
101-
or (rnd == RoundMode.TiesToAway and delta >= 0.5)
102-
or (rnd == RoundMode.TiesToEven and delta > 0.5)
103-
or (rnd == RoundMode.TiesToEven and delta == 0.5 and code_is_odd)
104-
or (rnd == RoundMode.Stochastic and delta > (0.5 + srbits) * 2.0**-srnumbits)
105-
)
106-
# fmt: on
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
107109

108110
if should_round_away:
109111
if fi.precision > 1:

0 commit comments

Comments
 (0)