C/C++ NaN 常量(字面量)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16691207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C/C++ NaN constant (literal)?
提问by exebook
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.
这是否可以将 a 分配NaN
给 adouble
或float
在 C/C++ 中?就像在JavaScript中你做的:a = NaN
。所以稍后您可以检查变量是否为数字。
回答by Mike Seymour
In C, NAN
is declared in <math.h>
.
在 C 中,NAN
在<math.h>
.
In C++, std::numeric_limits<double>::quiet_NaN()
is declared in <limits>
.
在 C++ 中,std::numeric_limits<double>::quiet_NaN()
在<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++.
但是为了检查一个值是否为 NaN,你不能将它与另一个 NaN 值进行比较。而是使用isnan()
来自<math.h>
于C,或std::isnan()
从<cmath>
C ++中。
回答by Shafik Yaghmour
As others have pointed out you are looking for std::numeric_limits<double>::quiet_NaN()
although I have to say I prefer the cppreference.comdocuments. Especially because this statement is a little vague:
正如其他人指出的那样,您正在寻找,std::numeric_limits<double>::quiet_NaN()
尽管我不得不说我更喜欢cppreference.com文档。尤其是因为这个说法有点含糊:
Only meaningful if std::numeric_limits::has_quiet_NaN == true.
仅当 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:
并且很容易弄清楚这在这个网站上意味着什么,如果你检查他们的部分std::numeric_limits::has_quiet_NaN
说:
This constant is meaningful for all floating-point types and is guaranteed to be true if std::numeric_limits::is_iec559 == true.
此常量对所有浮点类型均有意义,且若 std::numeric_limits::is_iec559 == true 则保证为 true。
which as explained hereif true
means your platform supports IEEE 754
standard. This previous threadexplains this should be true for most situations.
回答by languitar
This can be done using the numeric_limits in C++:
这可以使用 C++ 中的 numeric_limits 来完成:
http://www.cplusplus.com/reference/limits/numeric_limits/
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.
回答by chux - Reinstate Monica
Is this possible to assign a NaN to a double or float in C ...?
这是否可以将 NaN 分配给 C 中的双精度或浮点数...?
Yes, since C99, (C++11) <math.h>
offers the below functions:
是的,从 C99 开始,(C++11)<math.h>
提供以下功能:
#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.
这就像他们的strtod("NAN(n-char-sequence)",0)
同行和NAN
任务。
// 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)