@@ -73,22 +73,23 @@ def round_ndarray(
73
73
else :
74
74
code_is_odd = (isignificand != 0 ) & _isodd (expval + bias )
75
75
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
92
93
93
94
isignificand = np .where (should_round_away , isignificand + 1 , isignificand )
94
95
@@ -99,14 +100,15 @@ def round_ndarray(
99
100
if sat :
100
101
result = np .where (result > amax , amax , result )
101
102
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 )
110
112
111
113
result = np .where (finite_nonzero & put_amax_at , amax , result )
112
114
0 commit comments