acos function
dynamic
acos(
- dynamic x
Returns the arccosine of a number in radians.
Example:
print(acos(1)); // Output: 0.0
Example 2:
print(acos(Complex(1, 2))); // Output: 1.1437177404024204 - 1.528570919480998i
Implementation
dynamic acos(dynamic x) {
if (x is num) {
if (x >= -1 && x <= 1) {
return math.acos(x);
} else {
// Since math.acos does not support inputs outside [-1, 1], we need to use the complex formula
Complex z = Complex(x, 0);
dynamic result =
-Complex(0, 1) * log(z + Complex(0, 1) * sqrt(Complex(1, 0) - z * z));
return result;
}
} else if (x is Complex) {
// Using the formula: acos(z) = -i * log(z + i * sqrt(1 - z^2))
return -Complex(0, 1) *
log(x + Complex(0, 1) * sqrt(Complex(1, 0) - x * x));
} else {
throw ArgumentError('Input should be either num or Complex');
}
}