Skip to content

Commit 4635f5e

Browse files
committed
Simplify rounding logic
1 parent 5904633 commit 4635f5e

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

src/gfloat/round.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,19 @@ def round_float(
6262
result = 0.0
6363

6464
else:
65-
# Extract significand (mantissa) and exponent
65+
# Extract exponent
6666
expval = int(np.floor(np.log2(vpos)))
67-
fsignificand = vpos * 2.0**-expval
6867

69-
assert fsignificand >= 1.0 and fsignificand < 2.0
68+
assert expval > -1024 + p # not yet tested for float64 near-subnormals
7069

7170
# Effective precision, accounting for right shift for subnormal values
72-
biased_exp = expval + bias
7371
if fi.has_subnormals:
74-
effective_precision = t + min(biased_exp - 1, 0)
75-
else:
76-
effective_precision = t
72+
expval = max(expval, 1 - bias)
7773

7874
# Lift to "integer * 2^e"
79-
fsignificand *= 2.0**effective_precision
80-
expval -= effective_precision
75+
expval -= t
76+
77+
fsignificand = vpos * 2.0**-expval
8178

8279
# Round
8380
isignificand = math.floor(fsignificand)
@@ -101,8 +98,9 @@ def round_float(
10198
else:
10299
# All other modes tie to even
103100
if fi.precision == 1:
104-
# No significand bits
101+
# No bits in significand
105102
assert (isignificand == 1) or (isignificand == 0)
103+
biased_exp = expval + bias
106104
if _isodd(biased_exp):
107105
expval += 1
108106
else:

0 commit comments

Comments
 (0)