bpo-29282: Add math.fma(): fused multiply-add function by vstinner · Pull Request #17987 · python/cpython (original) (raw)

fma is a standard IEEE 754 function, so yes, the behaviour in corner cases is fully specified by IEEE 754

Sorry, I'm wrong: "fully" is incorrect. There's one case where both IEEE 754 and C99 refuse to specify the behaviour, namely fma(±0, ±inf, nan) and fma(±inf, ±0, nan). IEEE 754 says for these cases (see section 7.2(c) of IEEE 754-2019) that:

it is implementation defined whether the invalid operation exception is signaled

while C99 says in Annex F, 9.10.1:

fma(x, y, z) returns a NaN and optionally raises the invalid floating-point exception if one of x and y is infinite, the other is zero, and z is a NaN

(the emphasis above is mine). So for us, fma(0, inf, nan) could either raise ValueError or return a NaN. I don't know whether we should make a choice for Python in this case, or leave this system-defined.

My inclination would be to specify this in Python, and have Python return a NaN in this case, and not raise. @tim-one thoughts?