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 <stdio.h>     
#include <math.h>

const char *run_fpclassify(double n);
 
int main(int argc, char **argv)
{	
    printf("fpclassify(0.0 / 0.0) = %s\n", run_fpclassify(0.0 / 0.0));
	printf("fpclassify(1.0 / 0.0) = %s\n", run_fpclassify(1.0 / 0.0));
    printf("fpclassify(1.0) = %s\n", run_fpclassify(1.0));
    printf("fpclassify(-0.0) = %s\n", run_fpclassify(-0.0));
	
    return 0;
}
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";
    }
}

/*
run:
  
fpclassify(0.0 / 0.0) = NaN
fpclassify(1.0 / 0.0) = Infinite
fpclassify(1.0) = normal
fpclassify(-0.0) = zero

*/

 



answered Mar 18, 2016 by avibootz
edited Mar 18, 2016 by avibootz
...