4

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.

13
  • @Mysticial Does it stand for -1 times an indeterminate number? Because it could multiple things instead of just -∞... Like -1/0 Mar 28, 2013 at 2:49
  • Are you taking square-roots to draw your crescent shape? Can we see the formula? Perhaps you are taking the sqrt of a negative number? Mar 28, 2013 at 2:50
  • @AlbertRenshaw This is most certainly IEEE floating-point. -1/0 will evaluate to negative infinity. 0/0 will evaluate to NaN.
    – Mysticial
    Mar 28, 2013 at 2:50
  • I don't know what it meant. I assumed it was something similar, but I couldn't find anything about it online. So you advise checking my code for anywhere I might have divided by 0? Mar 28, 2013 at 2:51
  • 3
    -1.#IND means that the value is indeterminate. Take a look at the several types of indeterminate values linked, and then make sure your code isn't evaluating to one of them.
    – FThompson
    Mar 28, 2013 at 2:56

4 Answers 4

5

#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:

  • When n == 0 in the first iteration where j == 0 too, Xnew2 will be -1.#IND
  • When Xnew2 is greater than X, Ynew2 will be complex -> NaN
7
  • I think if n==0 than it would return #INF not #IND Mar 28, 2013 at 3:14
  • *UNLESS -j*(Y+R1) is also 0 because then it would be 0/0 which is indeterminate. Mar 28, 2013 at 3:14
  • 1
    It would first return #IND, then #INF because in the first iteration, j == 0. Thanks for pointing that out, it was ambiguous in my post
    – user123
    Mar 28, 2013 at 3:15
  • +1, I've also edited my equations below to account for this using conditional statements in the equation that change the denominator from 0 to 0.00001 if the denominator (n) is zero. --... Xnew2 = -j*(Y+R1)/((n)+((0.00001)*(n==0)); Mar 28, 2013 at 3:19
  • ^I've never programmed in C++ I'm assuming you can use conditional statements in equations to return binary booleans. If I'm way off with that code, sorry! Haha. Mar 28, 2013 at 3:21
2

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.

2

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.
1

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

4
  • it says that j is an undeclared identifier. any ideas? Mar 28, 2013 at 4:05
  • That just means that you never defined j as a variable. Mar 28, 2013 at 5:21
  • Also I just realized you could potentially get #INF in some spots why my code if you are dividing by X and X is 0... i.e. in the Ynew2 where it says (Xnew2/X) you could change it to (Xnew2/((n)+((0.00001)*(X==0))) and also do that for the other 3 times that appears in the code. Mar 28, 2013 at 5:22
  • It seems you defined j so that doesn't make much sense... I've never programmed in c++ but from some quick googling apparently your error lies somewhere in something called namespace std? google.com/… Mar 28, 2013 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.