Calculate the nth root?

Is there a way to compute the nth root from double to objective-c?

I could not find a suitable function.

+7
source share
3 answers

You should use the pow function:

pow(d, 1.0/n) 

enter image description here

+15
source

Mathematically, the nth root of x is equal to x of degree 1 / n.

I have no idea what objective-c syntax is, but basically you just want to use the power function with 1 / n as an exponent.

+3
source

For odd numbered roots (e.g. cubic) and negative numbers, the root result is well defined and negative, but just using pow(value, 1.0/n) doesn't work (you return NaN - not a number).

So use this instead:

 int f = (value < 0 && (n % 2 == 1)) ? -1 : 1; root = pow(value * f, 1.0/n) * f 
+1
source

All Articles