math.frexp

Nobody nobody at nowhere.invalid
Fri Jul 15 12:39:00 EDT 2016


On Fri, 15 Jul 2016 21:39:31 +1000, Steven D'Aprano wrote:

>         prod *= (m1*m2)

Should be:

	prod = m1*m2

or:
	prod *= m1

(in the latter case, there's no point in decomposing prod).

Of course, if the result overflows, it's going to overflow whether you use
the naive approach or frexp(); in the latter case, it's the 2.0**scale
which will overflow.

One advantage of processing the scale separately is that you can use e.g.

    return ((int(prod * 2**53) * (2**(scale-53))) if scale >= 53
            else prod * 2**scale)

which will return a (long) integer if the exponent is such that the
fractional bits are lost.




More information about the Python-list mailing list