hello I have to differenciate calculations in degrees and I have the following code but I doesn't return me the exact values. The only one right is the value of sin90 in degree = 1
//////***** DEGREES ******//////
var sinus = sin(90.0 * M_PI / 180)
var cosinus = cos(90 * M_PI / 180)
var tangent = tan(90 * M_PI / 180)
var arcsinus = asin(90 * M_PI / 180)
var arcosinus = acos(90 * M_PI / 180)
var arctangent = atan(90 * M_PI / 180)
What is the right operation to return the exact value for every operation in degree for cos, tan and their ARC functions?
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