Author mark.dickinson
Recipients aleax, mark.dickinson, remi.lapeyre, rhettinger, serhiy.storchaka, tim.peters, xtreak
Date 2018-12-31.17:02:30
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1546275750.77.0.247833819394.issue35606@roundup.psfhosted.org>
In-reply-to
Content
[Raymond]

> or to implement particular NaN/Inf handling not present in a naive implementation

On this subject, some effort has been made in the past to make (almost) all the math module functions behave consistently with respect to things like exceptions, overflow, infinities, nans, signed zeros, etc. (There are unfortunately some exceptions to the general rule, like math.pow.) If possible, I'd like to see any implementation of math.prod do the same; I'd prefer us to get this right initially rather than tweak the definition to make subtle possibly-breaking changes to the implementation later.

That is, the ideal behaviour would include things like:

(a) a product of finite floating-point (or convertible-to-float) numbers should raise an exception on overflow. Probably OverflowError, though ValueError wouldn't be indefensible either.

(b) a product involving infinities but no NaNs or zeros should return an appropriately-signed infinity (where the sign is determined by an xor of all the signs of the inputs)

(c) a product involving both infinities and zeros (but not NaNs) should raise ValueError

(d) a product involving a NaN at any point should just return NaN

The combination of these can get a bit awkward: for example, if a list starts with `[1e300, 1e300, ...]`, then it's not necessarily correct to raise `OverflowError` after processing the first two inputs, because if there's a NaN somewhere later in the list then that NaN should dominate the result. However, IEEE 754 allows some leeway here for its "Reduction operations" (section 9.4 of the to-be-superseded-any-day-now 2008 version of the standard), stating:

> Numerical results and exceptional behavior, including the invalid operation exception, may differ among implementations due to the precision of intermediates and the order of evaluation.

BTW, if we wanted to go for IEEE 754 compliance, the operation to implement would be "scaledProd", which takes a vector of inputs and returns a float along with an exponent; this allows taking products of long lists of floats without needing to worry about underflow or overflow. That's probably not what we want here, but the spec of scaledProd might help guide us with the implementation of prod.
History
Date User Action Args
2018-12-31 17:02:31mark.dickinsonsetrecipients: + mark.dickinson, tim.peters, aleax, rhettinger, serhiy.storchaka, remi.lapeyre, xtreak
2018-12-31 17:02:30mark.dickinsonsetmessageid: <1546275750.77.0.247833819394.issue35606@roundup.psfhosted.org>
2018-12-31 17:02:30mark.dickinsonlinkissue35606 messages
2018-12-31 17:02:30mark.dickinsoncreate