File tree Expand file tree Collapse file tree 3 files changed +603
-24
lines changed Expand file tree Collapse file tree 3 files changed +603
-24
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change @@ -88,10 +88,18 @@ def round_ndarray(
88
88
should_round_away = (delta > 0.5 ) | ((delta == 0.5 ) & code_is_odd )
89
89
case RoundMode .Stochastic :
90
90
assert srbits is not None
91
- should_round_away = delta > (0.5 + srbits ) * 2.0 ** - srnumbits
91
+ ## RTNE delta to srbits
92
+ d = delta * 2.0 ** srnumbits
93
+ floord = np .floor (d ).astype (np .int64 )
94
+ d = floord + ((d - floord > 0.5 ) | ((d - floord == 0.5 ) & _isodd (floord )))
95
+
96
+ should_round_away = d > srbits
92
97
case RoundMode .StochasticFast :
93
98
assert srbits is not None
94
99
should_round_away = delta > srbits * 2.0 ** - srnumbits
100
+ case RoundMode .StochasticNearly :
101
+ assert srbits is not None
102
+ should_round_away = delta > (2 * srbits + 1 ) * 2.0 ** - (1 + srnumbits )
95
103
96
104
isignificand = np .where (should_round_away , isignificand + 1 , isignificand )
97
105
Original file line number Diff line number Diff line change @@ -17,7 +17,14 @@ class RoundMode(Enum):
17
17
TiesToEven = 4 #: Round to nearest, ties to even
18
18
TiesToAway = 5 #: Round to nearest, ties away from zero
19
19
Stochastic = 6 #: Stochastic rounding
20
- StochasticFast = 7 #: Stochastic rounding - possibly faster, but biased.
20
+ StochasticFast = 7 #: Stochastic rounding - faster, but biased.
21
+ StochasticNearly = 8 #: Stochastic rounding - incorrect, see [Note 1].
22
+
23
+
24
+ # [Note 1]:
25
+ # StochasticNearly implements a stochastic rounding scheme that is unbiased in
26
+ # infinite precision, but biased when the quantity to be rounded is computed to
27
+ # a finite precision.
21
28
22
29
23
30
class FloatClass (Enum ):
You can’t perform that action at this time.
0 commit comments