감동, 마음이 움직이는 것
[c++] Infinite precision and FP Exceptions 본문
[c++] Infinite precision and FP Exceptions
Struggler J. 2016. 10. 6. 18:58This 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
'Tips (Utility, Computer Language, and etc.)' 카테고리의 다른 글
[Crank-Nicolson method/Tridiagonal matrix algorithm] (0) | 2016.10.13 |
---|---|
[c++] Max Min values of any variable type (0) | 2016.10.06 |
[Linux] sort the files from the modification time ls -lt (0) | 2016.10.05 |
Euler method 정리 (0) | 2016.10.03 |
[numerical algorithm] The 1D diffusion equation (0) | 2016.09.29 |