swift trigonometric functions (cos, tan, arcsin, arcos, arctan)

This is more a math problem than a Swift problem:

let sinus = sin(90.0 * Double.pi / 180)
print("Sinus \(sinus)")

let cosinus = cos(90 * Double.pi / 180)
print("Cosinus \(cosinus)")

let tangent = tan(90 * Double.pi / 180)
print("Tangent \(tangent)")

prints

Sinus 1.0
Cosinus 6.12323399573677e-17
Tangent 1.63312393531954e+16

Sinus of 90 degrees is 1 (correct)

Cosinus of 90 degrees is 0. The value 6e-17 is a very very small value, any sensible rounding would consider it equal to zero (correct). The fact that you can’t get exactly zero is due to rounding errors in the calculation.

Tangent of 90 degrees is not defined (sin/tan = 1/0, division by zero is not defined). If we had precise calculations, you would probably get an infinity. In this case we have 1 divided by 6e-17, which becomes a large number 1.6e16. The result is correct.

Regarding the inverse functions, note one thing – their parameters are neither in degrees or radians. Their result is in degrees/radians, for example:

let arcsinus = asin(1.0) * 180 / Double.pi
print("Arcsinus \(arcsinus)")

prints

Arcsinus 90.0

Leave a Comment