3

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.

t-mat
  • 63
  • 1
  • 6
Rory Duncan
  • 31
  • 1
  • 3
  • @Mysticial Does it stand for -1 times an indeterminate number? Because it could multiple things instead of just -∞... Like -1/0 – Albert Renshaw Mar 28 '13 at 02: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? – Albert Renshaw Mar 28 '13 at 02: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 '13 at 02: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? – Rory Duncan Mar 28 '13 at 02:51
  • @Mysticial But why would it say #IND instead of #INF ... I think it is an indefinite number like 0/0 or an imaginary number (or higher complex number). – Albert Renshaw Mar 28 '13 at 02:54
  • @AlbertRenshaw I dunno. I'm not sure if the standard even defines what the exact print text is supposed to be. – Mysticial Mar 28 '13 at 02:55
  • 3
    `-1.#IND` means that the value is [indeterminate](http://en.wikipedia.org/wiki/Indeterminate_form). 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 '13 at 02:56
  • Found this http://www.johndcook.com/IEEE_exceptions_in_cpp.html -1.#ind is use window's way of saying Nan (aka indeterminate) NOTE* the log of (in addition to the square root of) a negative number will also produce this result. – Albert Renshaw Mar 28 '13 at 02:57
  • possible duplicate of [What do 1.#INF00, -1.#IND00 and -1.#IND mean?](http://stackoverflow.com/questions/347920/what-do-1-inf00-1-ind00-and-1-ind-mean) – joce Mar 28 '13 at 03:07
  • @RoryDuncan I made an edit to my answer below, in the original answer I put the absolute value function int he wrong spot only our code. The new code should work, please test it out :o) Sorry for the inconvenience before! – Albert Renshaw Mar 28 '13 at 03:10
  • @RoryDuncan made another edit so that you don't get #INF or #IND for your x-values either! – Albert Renshaw Mar 28 '13 at 03:18
  • Question: Why are you putting that last bit of code in its own scope? (Nothing wrong with that, just wondering) – user123 Mar 28 '13 at 03:29
  • Your problem is you're dividing by 0. In your code, I have the feeling that `n` is equal to 0 when you get this value. – joce Mar 28 '13 at 04:35

4 Answers4

4

#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
user123
  • 8,970
  • 2
  • 31
  • 52
  • I think if n==0 than it would return #INF not #IND – Albert Renshaw Mar 28 '13 at 03:14
  • *UNLESS -j*(Y+R1) is also 0 because then it would be 0/0 which is indeterminate. – Albert Renshaw Mar 28 '13 at 03: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 '13 at 03: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));` – Albert Renshaw Mar 28 '13 at 03: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. – Albert Renshaw Mar 28 '13 at 03:21
  • I'm trying to verify my second assertion, and from what I can see, it is true so far that `pow` can't handle negative input when `exponent` is less than `1` – user123 Mar 28 '13 at 03:22
  • Less than 1 and greater than -1... Any number raised to the power of a fraction in the form of 1/x is the same as taking the x-root of a number... in this case -1^0.5 is the same as sqrt(-1) – Albert Renshaw Mar 28 '13 at 03:24
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.

Community
  • 1
  • 1
Muscles
  • 471
  • 4
  • 12
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.
ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
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

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
  • it says that j is an undeclared identifier. any ideas? – Rory Duncan Mar 28 '13 at 04:05
  • That just means that you never defined j as a variable. – Albert Renshaw Mar 28 '13 at 05: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. – Albert Renshaw Mar 28 '13 at 05: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`? http://www.google.com/search?client=safari&rls=en&q=undeclared+identifier+in+for+loop+namespace+std&ie=UTF-8&oe=UTF-8 – Albert Renshaw Mar 28 '13 at 05:25