What's wrong with (me or) the sine function?

The values I get out of the sine function are wrong, any ideas why?

float a = 180;
float b = sin(radians(a));
float c = sin(degrees(a));
println(b + " : " + c);

-8.742278E-8 : 0.5715942

a=0 (correct)
0 : 0

a=360 (what?!)
1.7484555E-7 : -0.93802774

a=2*PI (what!?)
0.10944261 : 0.9589157

Thanks!

Tagged:

Answers

  • float c = sin(degrees(a));
    

    converts a to degrees. but sin expects radians.

    float c = sin(a);
    
  • and if you're worried about a = 360

    1.7484555E-7

    read up on engineering notation - https://en.wikipedia.org/wiki/Engineering_notation

  • Answer ✓

    It seems like everything is fine wih the sin()-function. The problem might be that "PI" has only a limited number of digits, so the result of sin(0) and sin(2*PI) will never be equal, only close.

    If you need higher precision you can use javas own Math.PI which is a double.

    println(PI);
    println(Math.PI);
    
  • Ok, sin(PI) and sin(2PI) are just close to 0. but not exact. Thanks you guys.

  • there is a TWO_PI constant in processing as well

    println(sin(TWO_PI));
    

    1.7484555E-7

  • But Processing's TWO_PI is a float, unlike Math.PI which is a double.
    However, Processing's trigonometric functions want floats as input.

Sign In or Register to comment.