Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sinf() and cosf() in iOS app [duplicate]

Possible Duplicate:
Why doesn't sin() return the correct value here?

I am building an app which will perform math operations like sine (sinf()) and cosine (cost()) on floats. Now when I tried calculating the sinus on 90 (which is 1), I got a result of 0.89... which is trebly wrong. Is there a reason why the result is so off? is there a way to improve it? I have tried using double instead of float and got the same answer... Is it possible it's only like than when I run it in the simulator, and will work fine on a real iPhone? And lastly, will using a math parser like GCMathParser or DDMathParser improve there accuracy of the results?

Thanks, :]

like image 515
byteSlayer Avatar asked Nov 29 '22 02:11

byteSlayer


2 Answers

The sinf and cosf functions use radians. You have to convert first:

float result = sinf(yourDegree / 180 * M_PI);
like image 136
DrummerB Avatar answered Dec 22 '22 03:12

DrummerB


The problem is, it's taking the sine of 90 radians, not degrees. Try sin(M_PI_2) for 90°

The functions use radians, not degrees.

To convert degrees to radians use degrees * π / 180, so you can do something like sin( 90 * M_PI / 180 ) which reduces to π / 2 or M_PI_2

Why doesn't sin() return the correct value?

like image 43
Vineesh TP Avatar answered Dec 22 '22 03:12

Vineesh TP