cos function

dynamic cos(
  1. dynamic x
)

Returns the cosine of a number in radians.

Example 1:

print(cos(math.pi));  // Output: -1.0

Example 2:

var z = Complex(0, 0);
var z_cos = cos(z);

print(z_cos); // Output: 1.0 + 0.0i

Implementation

dynamic cos(dynamic x) {
  if (x is num) {
    return math.cos(x);
  } else if (x is Complex) {
    // Using the identity: cos(a + bi) = cos(a)cosh(b) - i*sin(a)sinh(b)
    return Complex(math.cos(x.real.value) * cosh(x.imaginary.getValue),
        -math.sin(x.real.value) * sinh(x.imaginary.getValue));
  } else {
    throw ArgumentError('Input should be either num or Complex');
  }
}