[c++] Infinite precision and FP Exceptions
This is the library for c/c++ that allows you to work with infinite precision FP arithmetic:
https://gmplib.org/
And this is an example of how to know when your arithmetic operation is overflowing or underflowing :
#include <cfenv>
// Clear previous FP exceptions
std::feclearexcept(FE_ALL_EXCEPT);
// Perform arithmetic operation
long double pc = 1./u;
// Retrieve the FP exception (if any)
int fp_exception = std::fetestexcept(FE_UNDERFLOW | FE_OVERFLOW);
// Check which kind of exception was rises by the arithmetic operation
if(fp_exception != 0)
{
std::ostringstream ssMsg;
ssMsg << "Warning: FP Exception whilst computing the pc: ";
if (fp_exception & FE_DIVBYZERO) ssMsg << " FE_DIVBYZERO";
if (fp_exception & FE_OVERFLOW) ssMsg << " FE_OVERFLOW";
if (fp_exception & FE_UNDERFLOW) ssMsg << " FE_UNDERFLOW";
}
Ref) http://en.cppreference.com/w/cpp/numeric/fenv/feclearexcept