-nan在C++中意味着什么?为什么pow(-4,-2.1)返回-nan?

Ion*_*zău 2 javascript c++ c++11

我理解为什么Math.pow(a, b)NaN a是负数,并且b在JavaScript中是非整数.其他编程语言也是如此.

但是,C++呢?

在JavaScript Math.pow(-4, -2.1)返回时NaN,在C++中pow (-4, -2.1)返回-nan.为什么?


例:

#include <iostream>
#include <math.h>

using namespace std;

int main () {
    cout << "(4)  ^ (2.1)  = " << pow (4,  -2.1) << endl; // 0.0544094
    cout << "(-4) ^ (-2.1) = " << pow (-4, -2.1) << endl; // -nan
    cout << "(-4) ^ (2.1)  = " << pow (-4,  2.1) << endl; // -nan
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

(4)  ^ (2.1)  = 0.0544094
(-4) ^ (-2.1) = -nan
(-4) ^ (2.1)  = -nan
Run Code Online (Sandbox Code Playgroud)

我使用编译代码g++.

$ g++ --version
g++ (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

试图使用在线工具编译它,但显示相同的结果:

小智 5

我能找到的唯一相关引用来自n1570.

实现可以给零和不是浮点数(例如无穷大和NaN)的值作为符号或者可以使它们保持未签名.如果此类值未签名,则本国际标准中检索标志的任何要求应产生未指明的标志,并且应忽略设置标志的任何要求.