Reputation: 41
I have some code that will allow me to draw a crescent moon shape, and have extracted the values to excel to be drawn. However in place of some numbers, there is -1.#IND
in place. Firstly if anyone could explain what this means, as Google came back with 0 links. And secondly if there is anyway to stop it from occurring.
There is a brief analogy of my code. I have lots more code besides this, however that is just calculating angles.
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;
}
MORE INFO
I'm now having the problem with this code.
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
AND
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;
I am having the problem drawing the crescent moon that I cannot get the two circles to have the same starting point? If this makes sense, I am trying to get two parts of circles to draw a crescent moon as such that they have the same start and end points. The only user input I have to work by is the radius and chosen center point.
If anyone has any suggestions on how to do this, it would be great, currently all I am getting more a 'half doughnut' shape, due to the circles not being connected.
Upvotes: 4
Views: 4387
Reputation: 17902
You are raising a negative number to the power of a non-inverse (i.e. 1/2, 0.5, 0.25, 0.333333, etc.) which results in a complex number. Like sqrt(-1) aka (-1)^(0.5)
Additionally you could also be equating 0/0 in two of your lines of code.
Use this code instead: (It takes the absolute value of your power's base (preventing negative values, thus preventing imaginary answers (complex numbers = NaN = -1.#IND)) It also prevents you from dividing by 0 if n == 0... in this event it adds 0.00001 to the denominator
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
}
*In the future avoid taking roots of negative numbers (which is the same as raising a negative number to a non-inverse-fraction power), avoid taking a logarithm of a negative number, and avoid dividing by 0 these all produce NaN (-1.#IND)
This code may be better (it uses conditional values to make your power's base zero if it is ever less than zero to prevent imaginary answers):
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
}
* I'd also like to point out what "Magtheridon96" mentioned in his answer. The code now makes sure n is not equal to zero otherwise you could be dividing by zero, although I think that would produce #INF not #IND... unless "-j(Y+R1)" is also zero, then it will be 0/0 which will be #IND
Upvotes: 1
Reputation: 9071
#IND
means an indetermined form.
What you have there is something known as 'Not a number'
or NaN for short.
Quoting from Wikipedia, generation is done by:
- Operations with a NaN as at least one operand.
- The divisions 0/0 and ±∞/±∞
- The multiplications 0×±∞ and ±∞×0
- The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions
- 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.
You're doing at least one of those things.
Edit:
After analyzing your code, these are 2 possibilities:
n == 0
in the first iteration where j == 0
too, Xnew2
will be -1.#IND
Xnew2
is greater than X
, Ynew2
will be complex -> NaN
Upvotes: 5
Reputation: 7769
This from the Wikipedia entry for IEEE 754 Nan:
There are three kinds of operations that can return 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.
Upvotes: 2
Reputation: 471
You are doing something illegal to a floating point number, such as taking the square root of a negative number. This is presumably on Windows. On Linux, you would get NaN (not a number) or inf. See -1 #IND Question for further information; the link provided in the second answer is helpful.
Upvotes: 2