mtommila/apfloat

hyperGeometricU - is it possible to get -Infinity, Infinity as a result?

Opened this issue · 5 comments

axkr commented

Example:

  hypergeometricU(3.0, 1.0, 0.0);

Should return Infinity

Can we have some special values for this case or a specialized exception?

Same for "ArithmeticException" with "Division by zero" text?

I suppose this would be in general a useful idea, having some kind of subclasses of ArithmeticException to indicate what happened (infinity, indeterminate, ...). Especially if a CAS system is to catch the exception and use it as a symbolic value somehow.

However there's a lot of places that would need to be changed (surely not just hypergeometricU) so it would be quite a lot of work. Also presumably some places would need a bit more logic to throw either "infinity" or "indeterminate" and not just the generic ArithmeticException, for example.

axkr commented

I suppose this would be in general a useful idea, having some kind of subclasses of ArithmeticException to indicate what happened (infinity, indeterminate, ...). Especially if a CAS system is to catch the exception and use it as a symbolic value somehow.

Does it make sense to use "checked exceptions" for these cases?

axkr commented

can OverflowException become a "checked exception", so that the user is forced to handle it?

No. It wouldn't be backwards compatible.

Overall, the classic Java exception paradigm is that checked exceptions are something that reasonably do happen, and must thus be handled by the user. Unchecked exceptions (i.e. those that extend RuntimeException) are exceptions that "should not happen", and overflow is more in the "should not happen" category (if e.g. also division by zero is).

So, I wouldn't change it even if it could be done in a backwards compatible way.

As noted in #38, it might be quite non-trivial to get the actual function value in the exception, because an ArithmeticException (or OverflowException) can be thrown anywhere in the middle of the evaluation of the algorithm for computing the function value, and it may not be the same value that would be the ultimate value of the function.

For example if your function is computed as 1/(1/x) then the first 1/x might throw +∞ but then the value of the function would be simply zero (i.e. 1/∞) and it shouldn't throw any exception at all.

Or for example xy is computed with exp(y * log(x)) and if y * log(x) overflows and throws an OverflowException but the real part of y * log(x) would be negative then the exp() would underflow to zero. But instead the function just throws OverflowException.