Mark Dickinson <dicki...@gmail.com> added the comment:

Here's code to illustrate the idea. It doesn't yet handle zeros, infinities or 
nans; that support would need to be added.

import math

def fprod(numbers):
    # Product of numbers, avoiding intermediate underflow and overflow.
    # Does not handle zeros, infinities or nans

    # Running product is acc_m * 2**acc_e
    acc_m, acc_e = float.fromhex("1p1000"), -1000

    count = 0
    for number in numbers:
        m, e = math.frexp(number)
        acc_m *= m
        acc_e += e
        if count == 1000:
            if acc_m < 1.0:
                acc_m = math.ldexp(acc_m, 1000)
                acc_e -= 1000
            count = 0

    return math.ldexp(acc_m, acc_e)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41458>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to