C NAN Number

Are you know how to create NAN number (Not a Number) in C? If you use Matlab, you can create NAN number by fill value variable with nan value (example : a=nan). What the the meaning of NAN number? NaN (Not a Number) is a value of the numeric data type representing an undefined or unrepresentable value, especially in floating-point calculations (from wikipedia). We can create NAN number by :

  • The divisions 0/0 and ±∞/±∞
  • The multiplications 0×±∞ and ±∞×0
  • The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions
  • etc

When use C, I want to create NAN number or to check NAN number. So, I create a simple C code to create NAN number and to check a number is NAN or not. Create new file and save with name ceknan.c. Copy this code to that file.

Compile this C Check NAN Number code in terminal with command :

gcc ceknan.c -o ceknan -lm

This is output from this C Check NAN Number program :

I use isnan function (function from C check NAN number is nan or not) and use variable compared function. So, if you want to check NAN number in C, you can use isnan function or compared a variable with itself. If that variable not equal with that variable itself. So, we can conclude that number is NAN.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.