Skip to content

Commit adef6f5

Browse files
committed
Use match
1 parent 9e3a543 commit adef6f5

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

src/gfloat/round_ndarray.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,23 @@ def round_ndarray(
7373
else:
7474
code_is_odd = (isignificand != 0) & _isodd(expval + bias)
7575

76-
if rnd == RoundMode.TowardZero:
77-
should_round_away = np.zeros_like(delta, dtype=bool)
78-
if rnd == RoundMode.TowardPositive:
79-
should_round_away = ~is_negative & (delta > 0)
80-
if rnd == RoundMode.TowardNegative:
81-
should_round_away = is_negative & (delta > 0)
82-
if rnd == RoundMode.TiesToAway:
83-
should_round_away = delta >= 0.5
84-
if rnd == RoundMode.TiesToEven:
85-
should_round_away = (delta > 0.5) | ((delta == 0.5) & code_is_odd)
86-
if rnd == RoundMode.Stochastic:
87-
assert srbits is not None
88-
should_round_away = delta > (0.5 + srbits) * 2.0**-srnumbits
89-
if rnd == RoundMode.StochasticFast:
90-
assert srbits is not None
91-
should_round_away = delta > srbits * 2.0**-srnumbits
76+
match rnd:
77+
case RoundMode.TowardZero:
78+
should_round_away = np.zeros_like(delta, dtype=bool)
79+
case RoundMode.TowardPositive:
80+
should_round_away = ~is_negative & (delta > 0)
81+
case RoundMode.TowardNegative:
82+
should_round_away = is_negative & (delta > 0)
83+
case RoundMode.TiesToAway:
84+
should_round_away = delta >= 0.5
85+
case RoundMode.TiesToEven:
86+
should_round_away = (delta > 0.5) | ((delta == 0.5) & code_is_odd)
87+
case RoundMode.Stochastic:
88+
assert srbits is not None
89+
should_round_away = delta > (0.5 + srbits) * 2.0**-srnumbits
90+
case RoundMode.StochasticFast:
91+
assert srbits is not None
92+
should_round_away = delta > srbits * 2.0**-srnumbits
9293

9394
isignificand = np.where(should_round_away, isignificand + 1, isignificand)
9495

@@ -99,14 +100,15 @@ def round_ndarray(
99100
if sat:
100101
result = np.where(result > amax, amax, result)
101102
else:
102-
if rnd == RoundMode.TowardNegative:
103-
put_amax_at = (result > amax) & ~is_negative
104-
elif rnd == RoundMode.TowardPositive:
105-
put_amax_at = (result > amax) & is_negative
106-
elif rnd == RoundMode.TowardZero:
107-
put_amax_at = result > amax
108-
else:
109-
put_amax_at = np.zeros_like(result, dtype=bool)
103+
match rnd:
104+
case RoundMode.TowardNegative:
105+
put_amax_at = (result > amax) & ~is_negative
106+
case RoundMode.TowardPositive:
107+
put_amax_at = (result > amax) & is_negative
108+
case RoundMode.TowardZero:
109+
put_amax_at = result > amax
110+
case _:
111+
put_amax_at = np.zeros_like(result, dtype=bool)
110112

111113
result = np.where(finite_nonzero & put_amax_at, amax, result)
112114

0 commit comments

Comments
 (0)