nthRoot function

dynamic nthRoot(
  1. dynamic x,
  2. double nth
)

Returns the nth root of a number.

Example:

// The cube root of 8 will be:
print(nthRoot(8, 3));  // Output: 2.0

Implementation

dynamic nthRoot(dynamic x, double nth) {
  if (x is num) {
    if (x >= 0) {
      return math.pow(x, 1 / nth);
    } else {
      return Complex(0, math.pow(-x, 1 / nth));
    }
  } else if (x is Number) {
    return x.nthRoot(nth);
  } else {
    throw ArgumentError('Input should be either num or Number');
  }
}