Search code examples
cmathsquare-root

How to get an arbitrary root in C?


C has sqrt() and cbrt(), but those are only the second and third roots. What if the root is an arbitrary number? What if I need the 57nth root?


Solution

  • Use the pow function, taking advantage of the fact that getting the 57 root is the same as raising to the power of 1 / 57.

    More generically, to get the y root of x:

    double result = pow(x, 1.0 / y);