Jump to content
  • Advertisement
Sign in to follow this  
Anri

Can anyone confirm the Sqrt(-1)?

This topic is 5116 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Looking around at different sources of info regarding "Quarternions", I have noticed that they all dodge the question "What is the square root of -1?". The simply reply with "i". But what about the value returned from such an operation when put through a computer? Not much is documented... So, insted of being lazy, I tried it myself. Using C++, I wrote the following code... int square = sqrt(-1); printf( "Square Root = %d," square ); ...the output was... Square Root = 0 ...hmm. So I did the same, but using doubles insted of integers and got the following answer... Square Root = -1.#IND00 ...and so I got my -1. Yippie. But what the devil is the .#IND00 part of it? Have I made a mistake here, or something I simply don't understand? Any contributions are welcome.

Share this post


Link to post
Share on other sites
Advertisement
You get x.#IND00 becouse sqrt(-1) is not defined in real numbers. The i you got in book is part of imaginry number (one of imaginary components).

Share this post


Link to post
Share on other sites
i is the square root of -1, they're not dodging the issue.

It's defined as pow(i, 2) = -1.

You sound like you think that -1 is the square root of -1 : but pow(-1, 2) is 1.

There are many different number systems, but the one you want for dealing with i is called the complex numbers. You might want to have a look here.

Jim.

Share this post


Link to post
Share on other sites
You just cannot calculate the sqrt(-1) using integers or doubles. It doesn't exist in the domain of integers or real numbers! A computer cannot calculate it. A human couldn't either if he was restricted to the domain of reals, either.

Share this post


Link to post
Share on other sites
sqrt(-1) (where sqrt takes and returns float or double objects)is undefined in the same way 1/0 or 1%0 are undefined reals. What does that mean? That there is no commonly accepted value of these numbers: no value can be inferred from the rules that allow computation of a value in other cases, and no value is correct in all the cases where the operation might occur.

Theoretically, an exception should be thrown the same way an exception is thrown for 1/0 and 1%0, but instead sqrt uses return-value error detection, so you have to detect manually if the returned value is correct or not.

For complex numbers, you can define an sqrt() function that applies to anything, because x^2 - z always has one or two solutions in the complex plane, and you can order the complex numbers arbitrarily to choose the highest of the two values. In that case, sqrt(-1 + 0*I) == I. But this is not the case if you stick to real numbers (and their float/double computer-world equivalents).

Share this post


Link to post
Share on other sites

#include <complex>
#include <iostream>

using namespace std;

int main()
{
complex<float> c = -1;
cout << sqrt(c) << endl;
}


Prints out (0,1) as expected. What exactly is your problem? [grin]

Share this post


Link to post
Share on other sites
The sqrt function knows not to even try negative numbers. If you use Newton's Method, which it most likely uses, then you repeatedly adjust a given function until it's answer is crossing the y axis. However, with negative values, it never nears the solution, and an infinite loop is created. try it with this, but take out the if at the top:



const double ACCURACY = 0.000001;

double sq(double x)
{
if (x < 0) return ERRORCODE_NONREAL;

//start out with half x
double xn = x/2;

double diff = x - xn;
double tdiff;
double fx;
double fpx;

//loop while the difference between each iteration
//is greater than the accuracy value
while ( abs(diff) > ACCURACY )
{
//the function is x^2 - x
fx = xn * xn - x;

//the derivative is 2x
fpx = 2 * xn;

tdiff = xn;

//apply newtons method
xn = xn - fx / fpx;

diff = tdiff - xn;
}
return xn;
}

Share this post


Link to post
Share on other sites
Back to the beginning: "-1.#IND" is a common printed representation for what the IEEE spec for floating point numbers calls a "quiet NaN", literally: quiet not a number. This is a non-signalling floating point value that represents the result of an invalid floating point operation, like taking the square root of -1.

Share this post


Link to post
Share on other sites
Re: JimPrice.

And so it would seem! o_0

That link you provided is indeed quite a find. I can't thank you enough. Cheers!

Re: Fruny.

MY PROBLEM!? YOU WANT TO KNOW MY "PROBLEM"?!?

My problem, my good Sir - is that I've been at this damn "problem" all day long since the crack of dawn! For some reason, I had decided to get of my arse and learn some Maths for a change! THAT is my problem!

I should have stayed in bed!

Oh, stop bawling - I'm just playing with you! In a Judge Judy kinda way...^_^

Seriously, your code still boggles my mind, but I start to get the idea that a complex number can be viewed in a component fashsion - similar to a vector. For that you get a cookie. You see - I'm not such a meanie, am I?

Re: The rest of you guys who replied!

This is more complex( no pun intended ) than I thought. Though I get the feeling that its simply one of those things that will click later as I go on learning.

One thing bothers me - is this concept considered to be a difficult concept in the world of Maths...or do I just plain suck?

Either way - thank you all for replying!



Share this post


Link to post
Share on other sites
Quote:

Seriously, your code still boggles my mind, but I start to get the idea that a complex number can be viewed in a component fashsion - similar to a vector.


Bang on.

Quote:

is this concept considered to be a difficult concept in the world of Maths...or do I just plain suck?


It's just something new to you - don't beat yourself up over it. You'll pick it up and in a short time you'll be replying to these types of threads yourself.

Oh yeah, and wait till you get to Quaternions...

Jim.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!