Author mark.dickinson
Recipients mark.dickinson, rhettinger, steven.daprano, tim.peters
Date 2021-11-26.21:46:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
> All the rounding has already happened at the point where ldexp is called, and the result of the ldexp call is exact.

Sketch of proof:

[Here]( we have:

    shift = Py_MAX(diff, DBL_MIN_EXP) - DBL_MANT_DIG - 2;

from which (assuming IEEE 754 as usual) shift >= -1076. (DBL_MIN_EXP = -1021, DBL_MANT_DIG = 53)

[Here]( we round away the last two or three bits of x, after which x is guaranteed to be a multiple of 4:

    x->ob_digit[0] = low & ~(2U*mask-1U);

Then after converting the PyLong x to a double dx with exactly the same value [here]( we make the ldexp call:

    result = ldexp(dx, (int)shift);

At this point dx is a multiple of 4 and shift >= -1076, so the result of the ldexp scaling is a multiple of 2**-1074, and in the case of a subnormal result, it's already exactly representable.

For the int/int division possibly not being correctly rounded on x87, see [here](

It won't affect _this_ application, but possibly we should fix this anyway. Though the progression of time is already effectively fixing it for us, as x87 becomes less and less relevant.
Date User Action Args
2021-11-26 21:46:19mark.dickinsonsetrecipients: + mark.dickinson, tim.peters, rhettinger, steven.daprano
2021-11-26 21:46:19mark.dickinsonsetmessageid: <>
2021-11-26 21:46:19mark.dickinsonlinkissue45876 messages
2021-11-26 21:46:19mark.dickinsoncreate