Difference between pow,sqrt,cbrt ?

Is there any difference between
1)pow(x,0.5) and sqrt(x).
2)pow(x,1.0/3) and cbrt(x).
Where x is some random integer.

Both of them are same. You can use any, its just a matter of choice.

Short answer

For square root there might be performance and/or accuracy differences based on your compiler and your standard library implementation.

For cube root most of that still applies, but on top of that pow and cbrt are not the same for negative x (cbrt is the correct one).

Long answer

Square root

Using gcc code with pow(x, 0.5) and sqrt(x) compile to different code which (unsurprisingly) calls pow and sqrt respectively. So you would have to check the difference between pow and sqrt. I would wager that because sqrt is the more specialized one it would be quicker and/or more accurate, but you should always measure it if you think that matters.

[gcc output @ godbolt][1]

Clang on the other hand actually converts the pow(x, 0.5) to a call to sqrt to produce very similar instructions in both cases, so I expect more or less identical performance.

[clang output @ godbolt][2]

Cube root

Here there is a substantial difference. pow(x, 1.0/3) does not work for negative x while cbrt does. This is the main reason why cbrt was added in the first place.

[gcc output @ godbolt][3]

[clang output @ godbolt][4]

Because of the difference neither gcc nor clang could convert pow(x, 1.0/3) to cbrt even if it wanted to. If x is positive you again end up with a similar question as before, is pow or cbrt faster and/or more accurate? Like before I would expect cbrt to have an edge, but you should measure it if you care about it.
[1]: https://godbolt.org/z/JqDZi3
[2]: https://godbolt.org/z/J49623
[3]: https://godbolt.org/z/P_3Iwi
[4]: https://godbolt.org/z/n2bKQg


Thanks a lot it helped me.

If you think it answers your question, like the post and accept it as an answer. :slight_smile:

Dont make answers wiki if its not needed. Wikified answers get no karma. This wikifying is reserved for editorials and announcments only.

@vijju123 I didn’t intend to do that, must have checked some box unintentionally. Thanks for fixing it!