c++初学者的Nan
nan in c++ for beginner
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
int main ()
{
float xstart = -2, xfinal = 2, h = 0.5;
float t, y;
cout << "X start = -2" << endl;
cout << "X final = 2" << endl;
cout << "step = 0.5" << endl << endl;
for ( float x = xstart; x <= xfinal; x+= h)
{
t= sqrt(pow(sin(x),2)) / sqrt(x - 4);
y= sqrt(2 * t + x);
cout << x << " | " << t << " | " << y << endl;
}
cout << endl;
}
将t和y变量输出为nan。我不知道如何修理它。这段代码只是问题的一个例子,不需要做任何其他的事情,除了修复nan。
t= sqrt(pow(sin(x),2)) / sqrt(x - 4);
在c++中,负数的平方根被定义为NAN (Not a number)。因为变量x
在范围[-2, 2]
中迭代,所以你永远不会让它不是NAN,这意味着使用它的所有计算都将到达NAN。
如果你希望sqrt返回复数,你需要使用std::complex
和它的各种数学函数
只要您继续在sqrt函数中使用负数,它就无法修复。(提示:x - 4总是负值或零)
相关文章:
- C++错误消息*成员参考.**初学者*
- g++的分段错误(在NaN上使用to_string两次时)
- 输出是NaN,如何
- 嵌套for循环C++的问题(初学者)
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 为什么我在输出端得到 nan?
- 提升反序列化对象具有 nan 或 -nan 值
- Is !NaN not a NaN?
- NaN 上的宇宙飞船操作员
- 初学者如何使用标准::any_of?
- C++ STL 排序会检查 NaN 吗?
- C++ 每次运行程序时我都会"nan"输出的问题
- 初学者问题:如何使用这些晦涩难懂的命令,例如C++中的'RenderWindow'
- 复制 -nan 表示浮点数,AVX __m256 复制后显示 0
- C++初学者:为什么我的编译器根据我的循环返回"name not found"?
- 如何使用 Node-addon-API 实现 node-nan 回调
- 初学者问题:如何访问此变量?
- 您好,我是C++初学者,有这种问题.它给了我倍增的错误
- 图形初学者C++(跳过控制台)?
- c++初学者的Nan