File tree Expand file tree Collapse file tree 1 file changed +8
-9
lines changed Expand file tree Collapse file tree 1 file changed +8
-9
lines changed Original file line number Diff line number Diff line change @@ -7813,11 +7813,6 @@ cdef double mpz_get_d_nearest(mpz_t x) except? -648555075988944.5:
7813
7813
# to have 54 bits remaining.
7814
7814
cdef mp_bitcnt_t shift = sx - 54
7815
7815
7816
- # Compute q = trunc(x / 2^shift) and let remainder_is_zero be True
7817
- # if and only if no truncation occurred.
7818
- cdef int remainder_is_zero
7819
- remainder_is_zero = mpz_divisible_2exp_p(x, shift)
7820
-
7821
7816
sig_on()
7822
7817
7823
7818
cdef mpz_t q
@@ -7842,12 +7837,16 @@ cdef double mpz_get_d_nearest(mpz_t x) except? -648555075988944.5:
7842
7837
# Round towards zero
7843
7838
pass
7844
7839
else :
7845
- if not remainder_is_zero :
7846
- # Remainder is nonzero: round away from zero
7840
+ if (q64 & 2 ) == 2 :
7841
+ # round to even and round away from zero gives the same result, no need to check
7847
7842
q64 += 1
7848
7843
else :
7849
- # Halfway case: round to even
7850
- q64 += (q64 & 2 ) - 1
7844
+ if mpz_divisible_2exp_p(x, shift):
7845
+ # Halfway case: round to even
7846
+ q64 -= 1
7847
+ else :
7848
+ # Remainder is nonzero: round away from zero
7849
+ q64 += 1
7851
7850
7852
7851
# The conversion of q64 to double is *exact*.
7853
7852
# This is because q64 is even and satisfies 2^53 <= q64 <= 2^54.
You can’t perform that action at this time.
0 commit comments