NaN in C++

M

mandydhaliwal

Hi all,

I want to put a NaN value in a double/float variable.

I am trying following code

double x = 12.7;
x/= 0;//Threotically x is a NaN now
Now when I Try _isnan(x) it returns that it is not a NaN.
bool isNan = (_isnan(x)!=0);


I am confused
Could you please guide on following
1) is this a right way to create NaN?
2) what is the platform independent cfunction to test a NaN value
 
D

Dave Rahardja

Hi all,

I want to put a NaN value in a double/float variable.

I am trying following code

double x = 12.7;
x/= 0;//Threotically x is a NaN now
Now when I Try _isnan(x) it returns that it is not a NaN.
bool isNan = (_isnan(x)!=0);


I am confused
Could you please guide on following
1) is this a right way to create NaN?
2) what is the platform independent cfunction to test a NaN value

Check out <limits>, specifically

std::numeric_limits<double>::has_quiet_NaN() and
std::numeric_limits<double>::quiet_NaN().

-dr
 
O

Owen Jacobson

Hi all,

I want to put a NaN value in a double/float variable.

I am trying following code

double x = 12.7;
x/= 0;//Threotically x is a NaN now
Now when I Try _isnan(x) it returns that it is not a NaN.
bool isNan = (_isnan(x)!=0);

Aside from Dave Rahardja's post, at this point (depending on
implementation) x may be +Inf, as distinct from NaN.
 
R

Rolf Magnus

Owen said:
Aside from Dave Rahardja's post, at this point (depending on
implementation) x may be +Inf, as distinct from NaN.

Actually, x may be anything or nothing at all (in case the program gets
terminated for example), since the result of a division by zero is
undefined.
 
D

Dave Rahardja

Actually, x may be anything or nothing at all (in case the program gets
terminated for example), since the result of a division by zero is
undefined.

Actually, if the implementation obeys IEEE-754/IEC-559 (i.e.
std::numeric_limits<double>::is_iec559 == true), x / 0.0 is defined to be +Inf
if x != 0.0.

From IEEE-754

7.2 Division by Zero

If the divisor is zero and the dividend is a finite nonzero number, then the
division by zero exception shall be signaled. The result, when no trap occurs,
shall be a correctly signed [Infinity] (6.3).

-dr
 
M

Mike Wahler

Dave Rahardja said:
Actually, x may be anything or nothing at all (in case the program gets
terminated for example), since the result of a division by zero is
undefined.

Actually, if the implementation obeys IEEE-754/IEC-559 (i.e.
std::numeric_limits<double>::is_iec559 == true), x / 0.0 is defined to be
+Inf
if x != 0.0.

From IEEE-754

7.2 Division by Zero

If the divisor is zero and the dividend is a finite nonzero number, then
the
division by zero exception shall be signaled. The result, when no trap
occurs,
shall be a correctly signed [Infinity] (6.3).

BUT:

IEEE-754 does not define the behavior of a C++ program.

ISO 14882 does.

-Mike
 
K

Kai-Uwe Bux

Mike said:
Dave Rahardja said:
Actually, if the implementation obeys IEEE-754/IEC-559 (i.e.
std::numeric_limits<double>::is_iec559 == true), x / 0.0 is defined to be
+Inf
if x != 0.0.

From IEEE-754

7.2 Division by Zero

If the divisor is zero and the dividend is a finite nonzero number, then
the
division by zero exception shall be signaled. The result, when no trap
occurs,
shall be a correctly signed [Infinity] (6.3).

BUT:

IEEE-754 does not define the behavior of a C++ program.

ISO 14882 does.

That's why he said: "if std::numeric_limits<double>::is_iec559 == true". In
that case, part of the meaning of a C++ program is defined by incorporation
of IEEE-754. Or the other way round, if the type double in a given
implementation does not adhere to IEEE-754, then compliance with the C++
standard requires std::numeric_limits<double>::is_iec559 to evaluate to
false.


Best

Kai-Uwe Bux
 
R

Rolf Magnus

Mike said:
From IEEE-754

7.2 Division by Zero

If the divisor is zero and the dividend is a finite nonzero number, then
the
division by zero exception shall be signaled. The result, when no trap
occurs,
shall be a correctly signed [Infinity] (6.3).

BUT:

IEEE-754 does not define the behavior of a C++ program.

ISO 14882 does.

However, that one says:

18.2.1.2 numeric_limits members [lib.numeric.limits.members]

[...]

static const bool is_iec559;

52 True if and only if the type adheres to IEC 559 standard.
 

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

Forum statistics

Threads
474,240
Messages
2,571,205
Members
47,843
Latest member
eicamotu

Latest Threads

Top