16

I was wondering why there is sqrt() function in C/c++ as we can achieve the same using

pow(x,0.5);

how is sqrt(x) different for pow(x,0.5) . Is there a specific reason of having sqrt function?

Alex
  • 1,178
  • 3
  • 9
  • 24

3 Answers3

19

I ran a test for you to check the performance of sqrt(x) and pow(x,0.5)

1.

for(int i=0;i<100000000;i++) 
    pow(double(i),0.5);

2.

for(int i=0;i<100000000;i++)
    sqrt(double(i));  

1st one took around 20 seconds where as 2nd one took around 2 seconds on my computer. So performance is way better. As other have already mentioned readability is other reason.

banarun
  • 2,305
  • 2
  • 23
  • 40
  • 1
    I got the opposite results... – Steve P. Jul 02 '13 at 05:48
  • I actually ran the test 3 times just to make sure. I didn't get much of variation. Please check again. – banarun Jul 02 '13 at 05:51
  • 1
    [Test it](http://ideone.com/dlwLGR). – Steve P. Jul 02 '13 at 05:58
  • @SteveP.: Is that code compiled with -O3? – Oliver Charlesworth Jul 02 '13 at 06:09
  • @steve P What processor are they using? The code which took more than 20 seconds on my PC ran in 0.6 seconds there. May be for larger values we can find the difference. – banarun Jul 02 '13 at 06:13
  • @OliCharlesworth, if you're referring to optimizations made, I reversed the two segments and still got the same results. – Steve P. Jul 02 '13 at 06:16
  • @banarun no idea. If they hadn't actually put this question on hold, I would've attempted to answer it based off of the source for the two... – Steve P. Jul 02 '13 at 06:17
  • @banarun try to reverse the two in your code on your computer and see what happens. – Steve P. Jul 02 '13 at 06:20
  • @Steve P I actually ran those two in separate programs – banarun Jul 02 '13 at 06:23
  • I think the pow function is optimised to sqrt() on ideone. when I compiled with -O3 there was no difference in the time taken for the two. – banarun Jul 02 '13 at 06:29
  • 1
    with -O3 and higher optimisations, benchmark such as yours will just get no-oped since no value is actually calculated... use int main() { double tot; clock_t t1; tot = 0.0; t1 = clock(); for( int i = 0; i < MAX; i++ ) tot += pow(double(i),-0.5); cout << "tot = " << tot << " time: " << clock()-t1 << "\n"; tot = 0.0; t1 = clock(); for( int i = 0; i < MAX; i++ ) tot += 1.0 / sqrt((double)i); cout << "tot = " << tot << " time: " << clock()-t1 << "\n"; return 0; } –  Jun 04 '14 at 18:49
  • 1
    @banarun on the other hands, my results are consistent with your; sqrt is about 10x-20x faster on my machine, even with division added to sqrt... –  Jun 04 '14 at 18:51
  • AFAIK, many modern CPUs have hardware accelerated sin, cos and sqrt functions – 0xB00B Dec 05 '21 at 16:25
6

I remember reading somewhere that sqrt() is a special case that is guaranteed by the IEEE specification to be rounded correctly. I'll look that up to find a source. It should be a little faster too, because it only has to handle one case.

Even if they were the same, it's nice to have a builtin alias for a commonly used function!

Edit: According to the IEEE-754, both the pow() function and sqrt() are supposed to be implemented such that the rounded value is the closest possible floating point representation to the real value. However, sqrt() should still be faster.

vroomfondel
  • 3,056
  • 1
  • 21
  • 32
5

Sure, if you think of only the mathematical equivalence...

But in terms of algorithms to compute the result, sqrt is specific to one thing whereas pow is generic.

So you could (rightly) assume that it's possible to write a faster function for sqrt than it is to write the generic pow function.

paddy
  • 60,864
  • 6
  • 61
  • 103