Skip to content

Commit ca391b8

Browse files
authored
Merge pull request #37 from graphcore-research/tidy-rounding-2
Simplify rounding for precision=1
2 parents cddcb4c + 33defcb commit ca391b8

File tree

2 files changed

+5
-19
lines changed

2 files changed

+5
-19
lines changed

src/gfloat/round.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,10 @@ def round_float(
108108
should_round_away = delta > (0.5 + srbits) * 2.0**-srnumbits
109109

110110
if should_round_away:
111-
if fi.precision > 1:
112-
isignificand += 1
113-
else:
114-
# Increment isignificand if zero, else increment exponent
115-
if isignificand == 0:
116-
isignificand = 1
117-
else:
118-
assert isignificand == 1
119-
expval += 1
111+
# This may increase isignificand to 2**p,
112+
# which would require special casing in encode,
113+
# but not here, where we reconstruct a rounded value.
114+
isignificand += 1
120115

121116
# Reconstruct rounded result to float
122117
result = isignificand * (2.0**expval)

src/gfloat/round_ndarray.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,7 @@ def round_ndarray(
8181
else:
8282
round_up = np.zeros_like(delta, dtype=bool)
8383

84-
if fi.precision > 1:
85-
isignificand = np.where(round_up, isignificand + 1, isignificand)
86-
else:
87-
# if isignificand == 0:
88-
# isignificand = 1
89-
# else:
90-
# assert isignificand == 1
91-
# expval += 1
92-
expval += round_up & (isignificand == 1)
93-
isignificand = np.where(round_up, 1, isignificand)
84+
isignificand = np.where(round_up, isignificand + 1, isignificand)
9485

9586
result = np.where(finite_nonzero, np.ldexp(isignificand, expval), absv)
9687

0 commit comments

Comments
 (0)