什么是"-1.#IND"?
What is '-1.#IND'?
我有一些代码,将允许我画一个月牙形,并已提取值excel要绘制。然而,在一些数字的地方,有-1.#IND
。首先,如果有人能解释一下这意味着什么,因为谷歌返回0链接。其次,如果有办法阻止它的发生。
我的代码有一个简短的类比。除此之外,我还有很多代码,但这只是计算角度。
for(int j=0; j<=n; j++)//calculation for angles and output displayed to user
{
Xnew2 = -j*(Y+R1)/n; //calculate x coordinate
Ynew2 = Y*(pow(1-(pow((Xnew2/X),2)),0.5));
if(abs(Ynew2) <= R1)
cout<<"n("<<Xnew2<<", "<<Ynew2<<")"<<endl;
}
更多信息
我现在有这个代码的问题。
for(int i=0; i<=n; i++) //calculation for angles and output displayed to user
{
Xnew = -i*(Y+R1)/n; //calculate x coordinate
Ynew = pow((((Y+R1)*(Y+R1)) - (Xnew*Xnew)), 0.5); //calculate y coordinate
和
for(int j=0; j<=n; j++)//calculation for angles and output displayed to user
{
Xnew2 = -j*(Y+R1)/((n)+((0.00001)*(n==0))); //calculate x coordinate
Ynew2 = Y*(pow(abs(1-(pow((Xnew2/X),2))),0.5));
if(abs(Ynew2) <= R1)
cout<<"n("<<Xnew2<<", "<<Ynew2<<")"<<endl;
我在画新月时遇到了问题,我不能让两个圆有相同的起点?如果这是有意义的,我试着让圆的两个部分画一个新月,这样它们就有相同的起点和终点。我唯一需要处理的用户输入是半径和选择的中心点。
如果有人对如何做到这一点有任何建议,那将是伟大的,目前所有我得到更多的"半甜甜圈"形状,由于圆圈没有连接。
#IND
表示不确定形式。
你得到的东西被称为'Not a number'
或简称NaN。
引用维基百科,生成是这样完成的:
- 以NaN作为至少一个操作数的操作。
- 划分0/0和±∞/±∞
- 乘法0×±∞和±∞×0
- 加法∞+(−∞)、(−∞)+∞和等效减法
- 负数的平方根。
- 负数的对数
- 小于−1或大于+1的一个数的反正弦或余弦。
你至少在做其中一件事。
编辑:在分析你的代码之后,有两种可能:
- 当
n == 0
在第一次迭代中出现j == 0
时,Xnew2
将是-1.#IND
- 当
Xnew2
大于X
时,Ynew2
将是复杂的->NaN
您正在对浮点数进行非法操作,例如取负数的平方根。这大概是在Windows上。在Linux上,您将得到NaN(不是数字)或inf。参见-1 #IND问题了解更多信息;第二个答案提供的链接很有帮助。
来自IEEE 754 Nan的维基百科条目:
可以返回NaN的操作有三种:
Operations with a NaN as at least one operand. Indeterminate forms The divisions 0/0 and ±∞/±∞ The multiplications 0×±∞ and ±∞×0 The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions The standard has alternative functions for powers: The standard pow function and the integer exponent pown function define 00, 1∞, and ∞0 as 1. The powr function defines all three indeterminate forms as invalid operations and so returns NaN. Real operations with complex results, for example: The square root of a negative number. The logarithm of a negative number The inverse sine or cosine of a number that is less than −1 or greater than +1.
您正在将一个负数取非逆的幂(即1/2,0.5,0.25,0.333333等),从而得到一个复数。比如sqrt(-1)也就是(-1)^(0.5)
另外,你也可以在两行代码中等于0/0。
用下面的代码代替:(它取你的功率基数的绝对值(防止负值,从而防止虚数(复数= NaN = -1.#IND))如果n == 0,它还可以防止你除以0…在本例中,它在分母
上加上0.00001。for(int j=0; j<=n; j++)//calculation for angles and output displayed to user
{
Xnew2 = -j*(Y+R1)/((n)+((0.00001)*(n==0)); //calculate x coordinate
Ynew2 = Y*(pow(abs(1-(pow((Xnew2/X),2))),0.5));
if(abs(Ynew2) <= R1)
cout<<"n("<<Xnew2<<", "<<Ynew2<<")"<<endl;
}
{
Xnew3 = -j*(Y+R1)/((n)+((0.00001)*(n==0)); //calculate x coordinate
Ynew3 = Y*(pow(abs(1-(pow((Xnew3/X),2))),0.5)); //calculate y coordinate
if(abs(Ynew3) <= R1)
cout<<"n("<<Xnew3<<", "<<Ynew3<<")"<<endl; //show x,y coordinates
}
*在未来避免对负数求根(这相当于将一个负数取一个非反比分数幂),避免对一个负数取对数,避免除以0——这些都会产生NaN (-1.#IND)
下面的代码可能会更好(它使用条件值使你的功率的基数为零,如果它小于零,以防止虚构的答案):
for(int j=0; j<=n; j++)//calculation for angles and output displayed to user
{
Xnew2 = -j*(Y+R1)/((n)+((0.00001)*(n==0)); //calculate x coordinate
Ynew2 = Y*(pow(((1-(pow((Xnew2/X),2)))*((1-(pow((Xnew2/X),2)))>(0))),0.5));
if(abs(Ynew2) <= R1)
cout<<"n("<<Xnew2<<", "<<Ynew2<<")"<<endl;
}
{
Xnew3 = -j*(Y+R1)/((n)+((0.00001)*(n==0)); //calculate x coordinate
Ynew3 = Y*(pow(((1-(pow((Xnew3/X),2))))*((1-(pow((Xnew3/X),2))))>(0))),0.5)); //calculate y coordinate
if(abs(Ynew3) <= R1)
cout<<"n("<<Xnew3<<", "<<Ynew3<<")"<<endl; //show x,y coordinates
}
*我还想指出"Magtheridon96"在他的回答中提到的内容。代码现在确保n不等于零,否则你可以除以零,尽管我认为这会产生#INF而不是#IND…除非"-j(Y+R1)"也是零,那么它将是0/0,即#IND
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 什么是"-1.#IND"?
- 什么是-1.#IND平均值(双流输出)