Search code examples
c++csqrt

Difference between sqrt(x) and pow(x,0.5)


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?


Solution

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