sqrt() vs x^0.5

Started by
6 comments, last by zackriggle 21 years, 1 month ago
Now, I understand that the sqrt() function is relatively slow. I also know that there are many ways to go about finding the square root of a number. Why doesn''t everyone just use X^0.5 where X is the number you want to find the square root of? Is it slow?
Advertisement
Because C++ does not have the ^ opperator. It only has the realtively slow pow function.

-~-The Cow of Darkness-~-
-~-The Cow of Darkness-~-
I don''t think that will work.. My compiler won''t compile if the value to the right of the ^ is a float. When I tried it as a whole number, it kept adding them together instead of performing an exponent operation.. Boggle

Kings of Chaos
As far as I know ^ is a bitwise XOR operator.
I realize that... but it was just a though I stumbled across during Algebra today [we were working on simplifying square roots...

I was just curious.
Let me explain ... just because a things is ONE operation in theoretical math ... does not exactly mean it is one efficient implementation in computer terms ... as saying, why not implement sqrt(x) as x^.5 power implies that they are different ... when as you pointed out they are not ... they are the same thing.

I want to give you a lot of credit for your attitude though, that way of looking at the world will lead you to find certain connections or optimizations others will miss ...

But in this case, the implementor of sqrt() will use whatever method is mathematically fastest ... and since he knows it will ALWAYS be a power of .5, his version will be FASTER than any pow function which takes a float ... because they cannot be optimized for this one case, where sqrt() can.
Xai, sqrt(x) and x^.5 are different in C/C++, which seems to be what he''s using. Did you mean sqrt(x) and pow(x, 2) are the same?
I think he meant that in math, sqrt(x) and x^.5 are the same. He then said in C++ that sqrt(x) is optimized since it will always be x^.5 whereas the pow() function has a variable exponent. So basically he said what you just said - sqrt(x) and pow(x, .5) are totally different.

quote:Original post by Dobbs
Xai, sqrt(x) and x^.5 are different in C/C++, which seems to be what he''s using. Did you mean sqrt(x) and pow(x, 2) are the same?


-YoshiXGXCX ''99

This topic is closed to new replies.

Advertisement