Skip to content

Commit 9aca7a1

Browse files
committed
Add StochasticOdd, another sweep on text.
1 parent 997152a commit 9aca7a1

File tree

5 files changed

+243
-117
lines changed

5 files changed

+243
-117
lines changed

docs/source/05-stochastic-rounding.ipynb

Lines changed: 223 additions & 116 deletions
Large diffs are not rendered by default.

src/gfloat/round.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ def round_float(
113113
(d - floord > 0.5) or ((d - floord == 0.5) and _isodd(floord))
114114
)
115115

116+
should_round_away = d > srbits
117+
case RoundMode.StochasticOdd:
118+
## RTNE delta to srbits
119+
d = delta * 2.0**srnumbits
120+
floord = np.floor(d).astype(np.int64)
121+
d = floord + (
122+
(d - floord > 0.5) or ((d - floord == 0.5) and ~_isodd(floord))
123+
)
124+
116125
should_round_away = d > srbits
117126
case RoundMode.StochasticFastest:
118127
should_round_away = delta > (0.5 + srbits) * 2.0**-srnumbits

src/gfloat/round_ndarray.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ def round_ndarray(
9494
dd = d - floord
9595
drnd = floord + (dd > 0.5) + ((dd == 0.5) & _isodd(floord))
9696

97+
should_round_away = drnd > srbits
98+
case RoundMode.StochasticOdd:
99+
assert srbits is not None
100+
## RTNO delta to srbits
101+
d = delta * 2.0**srnumbits
102+
floord = np.floor(d).astype(np.int64)
103+
dd = d - floord
104+
drnd = floord + (dd > 0.5) + ((dd == 0.5) & ~_isodd(floord))
105+
97106
should_round_away = drnd > srbits
98107
case RoundMode.StochasticFast:
99108
assert srbits is not None

src/gfloat/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class RoundMode(Enum):
1919
Stochastic = 6 #: Stochastic rounding
2020
StochasticFast = 7 #: Stochastic rounding - faster, but biased, see [Note 1].
2121
StochasticFastest = 8 #: Stochastic rounding - incorrect, see [Note 1].
22+
StochasticOdd = 9 #: Stochastic rounding, RTNO before comparison
2223

2324

2425
# [Note 1]:

test/test_round.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def test_stochastic_rounding(
541541
np.testing.assert_allclose(count_v1, expected_up_count, atol=atol)
542542

543543

544-
def test_stochastic_rounding_2() -> None:
544+
def test_stochastic_rounding_scalar_eq_array() -> None:
545545
fi = format_info_p3109(3)
546546

547547
v0 = decode_ndarray(fi, np.arange(255))

0 commit comments

Comments
 (0)