root method
- dynamic n
Returns the n-th root of this as a double
.
- Usage:
final n = 32.root(5);
- Only supported for positive numbers.
Important: The dot operator has higher precedence than the minus sign.
Therefore: -32.root(5) == -(32.root(5)) == -2
.
Whereas: (-32).root(5)
throws an error of
type ErrorOfType<InvalidFunctionParameter
.
Implementation
double root(n) {
if (isNegative) {
throw ErrorOfType<InvalidFunctionParameter>(
message: 'Can not calculate roots of negative numbers.',
invalidState: '$this < 0',
);
}
return math.exp(math.log(this) / n).toDouble();
}