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)
试图使用在线工具编译它,但显示相同的结果:
| 归档时间: |
|
| 查看次数: |
2743 次 |
| 最近记录: |