C/C++ NaN constant (literal)?

C++CNan

C++ Problem Overview


Is this possible to assign a NaN to a double or float in C/C++? Like in JavaScript you do: a = NaN. So later you can check if the variable is a number or no.

C++ Solutions


Solution 1 - C++

In C, NAN is declared in <math.h>.

In C++, std::numeric_limits<double>::quiet_NaN() is declared in <limits>.

But for checking whether a value is NaN, you can't compare it with another NaN value. Instead use isnan() from <math.h> in C, or std::isnan() from <cmath> in C++.

Solution 2 - C++

As others have pointed out you are looking for std::numeric_limits<double>::quiet_NaN() although I have to say I prefer the http://en.cppreference.com/w/cpp/types/numeric_limits/quiet_NaN">cppreference.com</a> documents. Especially because this statement is a little vague:

>Only meaningful if std::numeric_limits::has_quiet_NaN == true.

and it was simple to figure out what this means on this site, if you check their section on http://en.cppreference.com/w/cpp/types/numeric_limits/has_quiet_NaN">`std::numeric_limits::has_quiet_NaN`</a> it says:

>This constant is meaningful for all floating-point types and is guaranteed to be true if std::numeric_limits::is_iec559 == true.

which as explained http://en.cppreference.com/w/cpp/types/numeric_limits/is_iec559">here</a> if true means your platform supports IEEE 754 standard. This https://stackoverflow.com/questions/2234468/do-any-real-world-cpus-not-use-ieee-754">previous thread explains this should be true for most situations.

Solution 3 - C++

This can be done using the numeric_limits in C++:

http://www.cplusplus.com/reference/limits/numeric_limits/

These are the methods you probably want to look at:

infinity()	T	Representation of positive infinity, if available.
quiet_NaN()	T	Representation of quiet (non-signaling) "Not-a-Number", if available.
signaling_NaN()	T	Representation of signaling "Not-a-Number", if available.

Solution 4 - C++

> Is this possible to assign a NaN to a double or float in C ...?

Yes, since C99, (C++11) <math.h> offers the below functions:

#include <math.h>
double nan(const char *tagp);
float nanf(const char *tagp);
long double nanl(const char *tagp);

which are like their strtod("NAN(n-char-sequence)",0) counterparts and NAN for assignments.

// Sample C code
uint64_t u64;
double x;
x = nan("0x12345");
memcpy(&u64, &x, sizeof u64); printf("(%" PRIx64 ")\n", u64);
x = -strtod("NAN(6789A)",0);
memcpy(&u64, &x, sizeof u64); printf("(%" PRIx64 ")\n", u64);
x = NAN;
memcpy(&u64, &x, sizeof u64); printf("(%" PRIx64 ")\n", u64);

Sample output: (Implementation dependent)

(7ff8000000012345)
(fff000000006789a)
(7ff8000000000000)

> ... check if the variable is a number or no.

Use isnan(), std::isnan() from <math.h>, <cmath>.

Solution 5 - C++

yes, by the concept of pointer you can do it like this for an int variable:

int *a;
int b=0;
a=NULL; // or a=&b; for giving the value of b to a
if(a==NULL) 
  printf("NULL");
else
  printf(*a);

it is very simple and straitforward. it worked for me in Arduino IDE.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionexebookView Question on Stackoverflow
Solution 1 - C++Mike SeymourView Answer on Stackoverflow
Solution 2 - C++Shafik YaghmourView Answer on Stackoverflow
Solution 3 - C++languitarView Answer on Stackoverflow
Solution 4 - C++chux - Reinstate MonicaView Answer on Stackoverflow
Solution 5 - C++Mohammad Reza AbediniView Answer on Stackoverflow