153

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.

1

5 Answers 5

211

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++.

12
  • 29
    Or you can compare the number to itself – x == x returns false iff x is NaN.
    – Archie
    May 22, 2013 at 12:10
  • 8
    @Archie: I don't think that's guaranteed in either language. May 22, 2013 at 12:13
  • 5
    @MikeSeymour Not by the language standard but as far as I know it should work if the compiler claims to be IEEE compliant. May 22, 2013 at 12:23
  • 51
    @Pixelchemist: Indeed, it's an option if you need obfuscation but not portability. Personally, I prefer portability without obfuscation, so I won't suggest it myself. May 22, 2013 at 12:26
  • 11
    minor note: NAN is a float, not a double. link Aug 18, 2014 at 21:11
33

As others have pointed out you are looking for std::numeric_limits<double>::quiet_NaN() although I have to say I prefer the cppreference.com 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 std::numeric_limits::has_quiet_NaN 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 here if true means your platform supports IEEE 754 standard. This previous thread explains this should be true for most situations.

9

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.
1
7

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>.

6
  • 1
    What are the differences between outputs for different strings? Which one should we use in typical numerical code?
    – quant_dev
    Jul 26, 2020 at 19:55
  • 2
    @quant_dev x = NAN; fills most needs, else x = nan("0x12345"); is a clear way to specify a payload. Payload content differences is implementation defined. Commonly the MSBit of the 52-bit payload is a quiet / signaling flag.. See NAN. Sep 24, 2020 at 14:17
  • Great examples, thank you very much. When should we use for example x = nan("0x12345"); instead of a general NAN? I mean why do we need to say 0x12345 is a nan?
    – afp_2008
    Aug 1, 2022 at 22:20
  • 1
    @afp_2008 When not-a-number is implemented, there are usually many non-a-numbers. Often quiet and signaling NANs are implemented. When code wants to return something other than the default NAN, perhaps to signify quiet/signaling or some other meta data, nan("some_numeric_string"). This is a bit of a re-hash of this. If you need more, perhaps post a specific question? IIRC, one system tagged with the low bits of address to help facilitate where the NAN originated. Aug 1, 2022 at 22:26
  • 1
    @afp_2008 Curious, worked for me to get to my prior comment to quant_dev. Aug 1, 2022 at 22:30
-16

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.

4
  • 3
    NULL and NaN are actually not the same Sep 28, 2020 at 13:18
  • 1
    yes, they are basically different, but using NULL or NaN is same in the sense of checking if the variable is a number or no. Sep 29, 2020 at 9:02
  • 6
    "it worked for me" but is not related to the question at all. The question is how to assign NAN to a float or double. This answer does not contain NAN or any float or double.
    – wovano
    Sep 20, 2021 at 11:17
  • >using NULL or NaN is same in the sense of checking if the variable is a number or no. Not quite - many comparisons with NaN operate differently from this.
    – rsaxvc
    Feb 28 at 16:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.