#include <cfloat>
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
cout << fpclassify( 0.0 ) << " "; // 0 (=FP_ZERO )
cout << fpclassify(-0.0 ) << " "; // 0 (=FP_ZERO )
cout << fpclassify(-42.0 ) << " "; // -1 (=FP_NORMAL )
cout << fpclassify(DBL_MIN/2.0) << " "; // -2 (=FP_SUBNORMAL)
cout << fpclassify(NAN ) << " "; // 2 (=FP_NAN )
cout << fpclassify(INFINITY ) << " "; // 1 (=FP_INFINITE )
cout << ": ";
cout << (0.0 == -0.0 ) << " "; // 1
cout << (DBL_MIN/2.0 == DBL_MIN ) << " "; // 0
cout << (DBL_MIN/2.0 == DBL_MIN/4.0 ) << " "; // 0
cout << (DBL_MIN/DBL_MAX/2.0 == DBL_MIN/DBL_MAX/4.0) << " "; // 1
cout << endl;
return 0;
}
// Output: 0 0 -1 -2 2 1 : 1 0 0 1
Reference: http://en.cppreference.com/w/cpp/numeric/math/fpclassify
Tuesday, December 19, 2017
C++11: std::fpclassify()
C++11 added the function std::classify(). It classifies a floating point number. Here is an example:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment