Skip to content

Commit d67adb0

Browse files
committed
tidy
1 parent 25e7c68 commit d67adb0

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/gfloat/round.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def round_float(
4949
p = fi.precision
5050
bias = fi.expBias
5151

52-
if rnd == RoundMode.Stochastic:
52+
if rnd in (RoundMode.Stochastic, RoundMode.StochasticFast):
5353
if srbits >= 2**srnumbits:
5454
raise ValueError(f"srnumbits={srnumbits} >= 2**srnumbits={2**srnumbits}")
5555

@@ -94,20 +94,21 @@ def round_float(
9494
else (isignificand != 0 and _isodd(expval + bias))
9595
)
9696

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-
if rnd == RoundMode.StochasticFast:
110-
should_round_away = delta > srbits * 2.0**-srnumbits
97+
match rnd:
98+
case RoundMode.TowardZero:
99+
should_round_away = False
100+
case RoundMode.TowardPositive:
101+
should_round_away = not sign and delta > 0
102+
case RoundMode.TowardNegative:
103+
should_round_away = sign and delta > 0
104+
case RoundMode.TiesToAway:
105+
should_round_away = delta >= 0.5
106+
case RoundMode.TiesToEven:
107+
should_round_away = delta > 0.5 or (delta == 0.5 and code_is_odd)
108+
case RoundMode.Stochastic:
109+
should_round_away = delta > (0.5 + srbits) * 2.0**-srnumbits
110+
case RoundMode.StochasticFast:
111+
should_round_away = delta > srbits * 2.0**-srnumbits
111112

112113
if should_round_away:
113114
# This may increase isignificand to 2**p,

0 commit comments

Comments
 (0)