on floating-point numbers

Richard Damon Richard at damon-family.org
Sun Sep 5 23:21:14 EDT 2021



> On Sep 5, 2021, at 6:22 PM, Peter J. Holzer <hjp-python at hjp.at> wrote:
> 
> On 2021-09-04 10:01:23 -0400, Richard Damon wrote:
>>> On 9/4/21 9:40 AM, Hope Rouselle wrote:
>>> Hm, I think I see what you're saying.  You're saying multiplication and
>>> division in IEEE 754 is perfectly safe --- so long as the numbers you
>>> start with are accurately representable in IEEE 754 and assuming no
>>> overflow or underflow would occur.  (Addition and subtraction are not
>>> safe.)
>>> 
>> 
>> Addition and Subtraction are just as safe, as long as you stay within
>> the precision limits.
> 
> That depends a lot on what you call "safe", 
> 
> a * b / a will always be very close to b (unless there's an over- or
> underflow), but a + b - a can be quite different from b.
> 
> In general when analyzing a numerical algorithm you have to pay a lot
> more attention to addition and subtraction than to multiplication and
> division.
> 
>        hp
> 
> -- 
Yes, it depends on your definition of safe. If ‘close’ is good enough then multiplication is probably safer as the problems are in more extreme cases. If EXACT is the question, addition tends to be better. To have any chance, the numbers need to be somewhat low ‘precision’, which means the need to avoid arbitrary decimals. Once past that, as long as the numbers are of roughly the same magnitude, and are the sort of numbers you are apt to just write, you can tend to add a lot of them before you get enough bits to accumulate to have a problem. With multiplication, every multiply roughly adds the number of bits of precision, so you quickly run out, and one divide will have a chance to just end the process.

Remember, the question came up because the sum was’t associative because of fractional bits. That points to thinking of exact operations, and addition does better at that.


More information about the Python-list mailing list