Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to use fpclassify() function to categorizes floating point value into zero, subnormal, normal, infinite, NaN in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>

using namespace std;

const char *run_fpclassify(double n)
{
	switch (fpclassify(n))
	{
		case FP_INFINITE:  return "Infinite";
		case FP_NAN:       return "NaN";
		case FP_ZERO:      return "zero";
		case FP_NORMAL:    return "normal";
		case FP_SUBNORMAL: return "subnormal";
		default:           return "unknown";
	}
}

int main()
{
	cout << "fpclassify(0.0 / 1.0) = " << run_fpclassify(0.0 / 1.0) << endl;
	cout << "fpclassify(1.0 / 1.0) = " << run_fpclassify(1.0 / 1.0) << endl;
	cout << "fpclassify(1.0) = " << run_fpclassify(1.0) << endl;
	cout << "fpclassify(-0.0) = " << run_fpclassify(-0.0) << endl;

	return 0;
}


/*
run:

fpclassify(0.0 / 1.0) = zero
fpclassify(1.0 / 1.0) = normal
fpclassify(1.0) = normal
fpclassify(-0.0) = zero

*/

 



answered Mar 18, 2016 by avibootz
...