Skip to content

Commit 7998d08

Browse files
committed
A possible tidying
1 parent 0bb857a commit 7998d08

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

src/gfloat/round.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,40 +88,33 @@ def round_float(
8888
isignificand = math.floor(fsignificand)
8989
delta = fsignificand - isignificand
9090

91+
code_is_odd = (
92+
_isodd(isignificand)
93+
if fi.precision > 1
94+
else (isignificand != 0 and _isodd(expval + bias))
95+
)
96+
9197
# fmt: off
92-
if (
98+
should_round_away = (
9399
(rnd == RoundMode.TowardPositive and not sign and delta > 0)
94100
or (rnd == RoundMode.TowardNegative and sign and delta > 0)
95101
or (rnd == RoundMode.TiesToAway and delta >= 0.5)
96102
or (rnd == RoundMode.TiesToEven and delta > 0.5)
97-
or (rnd == RoundMode.TiesToEven and delta == 0.5 and _isodd(isignificand))
103+
or (rnd == RoundMode.TiesToEven and delta == 0.5 and code_is_odd)
98104
or (rnd == RoundMode.Stochastic and delta > (0.5 + srbits) * 2.0**-srnumbits)
99-
):
100-
isignificand += 1
101-
102-
## Special case for Precision=1, all-log format with zero.
103-
# The logic is simply duplicated (and isignificand overwritten) for clarity.
104-
if fi.precision == 1:
105-
isignificand = math.floor(fsignificand)
106-
code_is_odd = isignificand != 0 and _isodd(expval + bias)
107-
if (
108-
(rnd == RoundMode.TowardPositive and not sign and delta > 0)
109-
or (rnd == RoundMode.TowardNegative and sign and delta > 0)
110-
or (rnd == RoundMode.TiesToAway and delta >= 0.5)
111-
or (rnd == RoundMode.TiesToEven and delta > 0.5)
112-
or (rnd == RoundMode.TiesToEven and delta == 0.5 and code_is_odd)
113-
or (rnd == RoundMode.Stochastic and delta > (0.5 + srbits) * 2.0**-srnumbits)
114-
):
115-
# Go to nextUp.
116-
# Increment isignificand if zero,
117-
# else increment exponent
105+
)
106+
# fmt: on
107+
108+
if should_round_away:
109+
if fi.precision > 1:
110+
isignificand += 1
111+
else:
112+
# Increment isignificand if zero, else increment exponent
118113
if isignificand == 0:
119114
isignificand = 1
120115
else:
121116
assert isignificand == 1
122117
expval += 1
123-
## End special case for Precision=1.
124-
# fmt: on
125118

126119
# Reconstruct rounded result to float
127120
result = isignificand * (2.0**expval)

0 commit comments

Comments
 (0)