Fix undefined behavior for infinite arguments in ellint phi
Prevents undefined behavior in ellint phi by handling infinite inputs and throwing a domain error.
This commit addresses undefined behavior in the libstdc++ implementation of __ellint_1, __ellint_2, and __ellint_3 when the ‘phi’ argument is infinity. Previously, the function would compute std::floor(infinity) and cast it to an int, which is undefined behavior since infinity is outside the range of int. This can lead to incorrect code generation, such as infinite loops. The fix replaces this with a check for non-finite ‘phi’ values, throwing a std::domain_error to conform to mathematical function standards and prevent UB.
In Details
In the TR1/ell_integral.tcc implementation of __ellint_1, __ellint_2, and __ellint_3, non-finite phi inputs (specifically +/-infinity) previously caused undefined behavior due to casting std::floor(infinity) to an integer type. This commit guards against this by checking for __builtin_isinf(phi) and throwing std::domain_error, aligning with common practice for handling invalid inputs to mathematical functions.
- libstdc++
- The standard C++ library implementation used by GCC. It provides containers, algorithms, I/O streams, and other essential C++ features.
- ellint phi
- Refers to the 'phi' parameter in elliptic integrals, specifically the incomplete elliptic integrals of the first, second, and third kinds. These are special mathematical functions.
- Undefined Behavior (UB)
- Behavior that results from an erroneous program construct or incorrect use of a standard API. The C and C++ standards do not specify what must happen in such cases, and compilers are free to assume UB does not occur, potentially leading to unexpected results or optimizations.
- std::domain_error
- A standard C++ exception type thrown when a mathematical function receives an argument outside its valid domain.
- C++98
- The 1998 version of the C++ standard, a foundational release for the language.
- NaN
- Not a Number. A special floating-point value representing an undefined or unrepresentable numerical result.