math.frexp

Steven D'Aprano steve at pearwood.info
Sat Jul 16 06:21:13 EDT 2016


On Sat, 16 Jul 2016 06:24 am, Paul Rubin wrote:

> Steven D'Aprano <steve at pearwood.info> writes:
>> But this can give some protection against overflow of intermediate
>> values.
> 
> Might be simplest to just add the logarithms.  Look up Kahan summation
> for how to do that while minimizing loss of precision.

Simplest, but least accurate, even with Kahan summation or equivalent. Even
a naive implementation of product does better:

py> from operator import mul
py> reduce(mul, [1.0, 2.0, 3.0, 4.0, 5.0])
120.0
py> math.exp(math.fsum(math.log(x) for x in [1.0, 2.0, 3.0, 4.0, 5.0]))
119.99999999999997


That second answer might be good enough for getting an astronaut to the
Moon, but it's not good enough to get them back again *wink*



-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list