Makro sprawdzające

Składnia edytuj

  • Parametr: arg - wartość zmiennoprzecinkowa
  • Wartość zwracane: Niezerowa wartość całkowita, jeśli arg jest normalny, ​0​ w przeciwnym razie.


kod żródłowy edytuj

Szukamy:

  grep -i isnormal /usr/include/math.h
#  define isnormal(x) __builtin_isnormal (x)
#  define isnormal(x) (fpclassify (x) == FP_NORMAL)

Czyli jest to:

Przykład edytuj

Pierwszy przykład[1]

#include <stdio.h>
#include <math.h>
#include <float.h>
 
int main(void)
{
    printf("isnormal(NAN)         = %d\n", isnormal(NAN));
    printf("isnormal(INFINITY)    = %d\n", isnormal(INFINITY));
    printf("isnormal(0.0)         = %d\n", isnormal(0.0));
    printf("isnormal(DBL_MIN/2.0) = %d\n", isnormal(DBL_MIN/2.0));
    printf("isnormal(1.0)         = %d\n", isnormal(1.0));
}


Kompilacja


Wynik:

isnormal(NAN)         = 0
isnormal(INFINITY)    = 0
isnormal(0.0)         = 0
isnormal(DBL_MIN/2.0) = 0
isnormal(1.0)         = 1

Drugi przykład

/* 
isnormal  example 

ISO C99
http://www.cplusplus.com/reference/cmath/isnormal/
http://www.gnu.org/software/libc/manual/html_node/Floating-Point-Classes.html
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_math.html


compile with: 
gcc -std=c99 -lm s.c

run :
./a.out

*/
#include <stdio.h>      /* printf */
#include <math.h>       /* isnormal, fpclassify */



int TestNumber(double x)
{
  int f; // flag

  f= isnormal(x);
  if (f) 
    printf ( "number %.16e is normal \n",x);
    else printf ("number %.16e is not normal \n",x);

  return f;

}

//----------------------------

int main()
{

  double d = 1.0;
  double MinNormal= 1.0; 
  int f = 0;


  d = 1.0 ; // normal
  f = TestNumber(d);
  
 
  while (f)
  {
  	MinNormal=d;
   	d /=2.0;
   	f = TestNumber(d);
   }
 


  printf ("\n\number %.16f = %.16eis minimal normal \n",MinNormal, MinNormal);
  printf ("number %.16e is not normal \n",d);
  


  return 0;
}


Źródła edytuj

  1. cppreference c isnormal