NaN question

C

Cephas

How do i write an if statement that checks the results of a operation
to see if its nan (not a number) or not? *using g++, a linux complier*

I tried
double x;
if (x == nan)
{
code here
}

but nan isnt a built in defined varible.
I cant say:
if (x == 0)
because that means it holds a value of a number.

I am performing a complex mathimatical equation on diffrent variables
and need to create a if statement for the times that my equation
returns NaN.

Thanks!
-Cephas
 
T

Thomas Wintschel

Cephas said:
How do i write an if statement that checks the results of a operation
to see if its nan (not a number) or not? *using g++, a linux complier*

I tried
double x;
if (x == nan)
{
code here
}

but nan isnt a built in defined varible.
I cant say:
if (x == 0)
because that means it holds a value of a number.

I am performing a complex mathimatical equation on diffrent variables
and need to create a if statement for the times that my equation
returns NaN.

Thanks!
-Cephas

there should be an isnan or isNaN function kicking around somewhere that you
can use to test a given value

Tom
 
S

Sergei Zyablov

How do i write an if statement that checks the results of a operation
to see if its nan (not a number) or not? *using g++, a linux complier*

//...
#include <math.h>
//...
if( isnan(0/0) )
cout << "0/0 = nan" << endl;
else
cout << "0/0 = " << (0/0) << endl;

Try
man isnan
for isinf() and finite().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top