Sin() cos() functions acting strangely

Hello,
Can’t understand this:

  • In one simple sketch sin(30) and cos(30) produce results that appear incorrect while the exact same code in another sketch (from the Examples section) returns the correct result.

  • Here’s the code in each sketch (placed in the Setup section)
    print(sin(30));
    print(cos(30));

  • Screenshot of the erroneous sketch + console output is attached (as a newbie I can only attach one pic). Thx for whatever insights anyone can provide!

1 Like

Hello, you need to pay attention if you are working in degrees or in radians. You can change the mode with the angleMode() function.

3 Likes

Hi! Default setting for both Processing and p5.js are radians. They are similar to the ° (degrees (360)) we use in Europe, however, they are between 0 and PI*2 (TWO_PI). There are several ways to make turn degrees into radians;

  • use formula: degrees*TWO_PI/360
  • use map function: map(degrees,0,360,0,TWO_PI)
  • use inbuilt functions: radians(degrees)
  • use angleMode (p5 only); angleMode(DEGREES);

I hope this helps

1 Like

Hello @psemme,

You should always refer to the references:
reference | p5.js

In this case sin() and cos() also reference angleMode() which provides a good example.

References:
image

Other references:

Have fun!

:)

1 Like

Ok - I’m embarrassed. I should have figured that out! Thx!

1 Like

Thank you! Don’t know why I didn’t remember this basic setting!

1 Like

Thank you! Appreciate the quick response.

2 Likes