What you learn today
- Radians and degrees
- deg2rad and rad2deg
- Sin, cos, tan
The pro level starts with some math you need for solar panels, trackers and rotations: trigonometric functions. Do not worry – you do not have to prove anything, just know what goes in and what comes out.
Degrees and radians
People measure angles in degrees: a circle has 360°. The trigonometric functions on the chip work in radians: a circle has 2π, about 6.283. A right angle is 90° or π/2.
| Degrees | Radians |
|---|---|
| 0° | 0 |
| 90° | π/2 ≈ 1.571 |
| 180° | π ≈ 3.142 |
| 360° | 2π ≈ 6.283 |
There are two built-in constants for converting:
mul rad deg deg2rad # degrees -> radians
mul deg rad rad2deg # radians -> degrees
Besides that the chip knows pi and tau (= 2π).
sin, cos, tan
sin val rad # sine
cos val rad # cosine
tan val rad # tangent
| Angle | sin | cos |
|---|---|---|
| 0° | 0 | 1 |
| 30° | 0.5 | 0.866 |
| 90° | 1 | 0 |
| 180° | 0 | −1 |
Sine and cosine always lie between −1 and 1. What is that good for? A solar panel's efficiency depends on a trigonometric function: according to the game code it is 1 − 2 · sin(deviation / 2). At 30° deviation a little under half is left, at 60° nothing at all – more on that on day 186. And if you need a point on a circle – say for a display with a pointer – you get x and y from cosine and sine.
Never feed degrees into the trigonometric functions. sin val 90 computes the sine of 90 radians – that is more than 14 full turns and gives about 0.894 instead of 1.
In the game deg2rad is only accurate to about seven digits (0.01745329238…). For panels and displays that is more than enough; for exact comparisons like seq you should use a tolerance anyway.
tan with care
The tangent becomes huge at 90° – it divides by almost zero there. For angles from two values you will use atan2 tomorrow instead.
Exercise
Task
A dial named “Winkel” delivers an angle in degrees via Setting. Two LED displays on the data network are named “Sinus” and “Kosinus”.
- “Sinus” shows sin(angle) · 100
- “Kosinus” shows cos(angle) · 100
Convert the angle to radians first.
Hints
Hint 1
First mul rad deg deg2rad.
Hint 2
Then sin val rad, multiply by 100 and write it to “Sinus” with sbn.
Hint 3
For the cosine the same with cos.
Quiz
4 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in