|
1 | | - # sin(x) - Your Chill Math Buddy |
2 | | - |
3 | | -Hey! Sine takes an angle and gives you a number—like a height on a circle. Great for coding, music, or math fun. Let’s check it out! |
4 | | - |
5 | | ---- |
6 | | - |
7 | | -## What You Give It |
8 | | - |
9 | | -- **x**: a number |
10 | | - Angle in radians (not degrees—a full spin’s about 6.28). Works with any number, even `NaN` or infinity. |
11 | | - |
12 | | -## What You Get Back |
13 | | - |
14 | | -- **y**: a number |
15 | | - Between -1 and 1. `NaN` if `x` is weird. |
16 | | - |
17 | | -## What It’s Good For |
18 | | - |
19 | | -- Smooth game moves |
20 | | -- Sound tweaks |
21 | | -- Shape math |
22 | | - |
23 | | ---- |
24 | | -__________________________________________________________________________ |
25 | | -## Give These a Go |
26 | | - |
27 | | -- `sin(0.0)` → `0.0` (no turn) |
28 | | -- `sin( {{alias:@stdlib/constants/float64/pi}} / 2 )` → `1.0` (90°—top!) |
29 | | -- `sin( -{{alias:@stdlib/constants/float64/pi}} / 6 )` → `-0.5` (-30° dip) |
30 | | -- `sin( 0.785 )` → ~`0.707` (45°—even) |
31 | | -- `sin( {{alias:@stdlib/constants/float64/pi}} )` → `0.0` (half spin) |
32 | | -- `sin( Infinity )` → `NaN` (too big!) |
33 | | - |
34 | | ---- |
35 | | -__________________________________________________________________________ |
36 | | -## Fun Code to Try |
37 | | - |
38 | | -### For C Folks |
39 | | -code-> |
40 | | -- **Add**: `#include <math.h>` |
41 | | -- **Bounce a Ball**: |
42 | | - ```c |
43 | | - #include <math.h> |
44 | | - #include <stdio.h> |
45 | | - int main() { |
46 | | - for (int step = 0; step < 5; step++) { |
47 | | - printf("Height %d: %.2f\n", step, sin(step * 0.5)); |
48 | | - } |
49 | | - return 0; |
50 | | - } |
51 | | - |
52 | | - gcc file.c -lm—prints 0.00, 0.48, 0.91... |
53 | | -______________________________________________________________________ |
54 | | -### For JavaScript Folks |
55 | | -Use: Math.sin() |
56 | | -Fade a Light: |
57 | | -code-> |
58 | | -for (let time = 0; time < 5; time += 0.5) { |
59 | | - let glow = (Math.sin(time) + 1) / 2; |
60 | | - console.log(`Glow ${time}: ${glow.toFixed(2)}`); |
61 | | -} |
62 | | -output-> |
63 | | -Shows 0.50, 0.73, 0.91—smooth! |
64 | | -__________________________________________________________________ |
65 | | -Extra: Mix Waves |
66 | | -code-> |
67 | | -let mix = Math.sin(1.5) + Math.sin(2.5); |
68 | | -console.log("Wave mix:", mix.toFixed(3)); // ~ -0.589 |
69 | | -__________________________________________________________________ |
70 | | -Quick Bits |
71 | | -Loops: Repeats every 6.28. sin(100) = sin(100 % 6.28). |
72 | | -Flips: sin(-x) = -sin(x). |
73 | | -Stays Simple: -1 to 1 only. |
74 | | -Spot-On: sin(π/2) = 1, sin(0) = 0. |
75 | | -___________________________________________________________________ |
76 | | -Easy Tips |
77 | | -Degrees?: Multiply by 3.14 / 180. sin(60 * 3.14 / 180) ≈ 0.866. |
78 | | -Save It: Store repeats—like let h = sin(1.5). |
79 | | -Handy: sin(3.14 / 3) ≈ 0.866 (60°). |
80 | | -___________________________________________________________________ |
81 | | -Other Pals |
82 | | -cos(x): Wave teammate. |
83 | | -tan(x): Slope helper. |
84 | | -{{alias:@stdlib/constants/float64/pi}}: 3.14159. |
| 1 | +sin( x ) |
| 2 | + Computes the sine of a number. |
| 3 | + |
| 4 | + Parameters |
| 5 | + ---------- |
| 6 | + x: number |
| 7 | + Input value (in radians). Can be any number, including `NaN` or `Infinity`. |
| 8 | + |
| 9 | + Returns |
| 10 | + ------- |
| 11 | + y: number |
| 12 | + Sine of the input value, a number between -1 and 1. Returns `NaN` for invalid inputs like `NaN` or `Infinity`. |
| 13 | + |
| 14 | + Notes |
| 15 | + ----- |
| 16 | + - The sine function is periodic with a period of 2π (approximately 6.28 radians), so `sin(x) = sin(x % 6.28)`. |
| 17 | + - The function satisfies the identity `sin(-x) = -sin(x)`. |
| 18 | + - Useful for smooth game movements, sound wave adjustments, and geometric calculations. |
| 19 | + - To convert degrees to radians, multiply by π/180 (e.g., `sin(60 * 3.14 / 180) ≈ 0.8660` for 60 degrees). |
| 20 | + - For repeated calculations, store the result to avoid recomputing (e.g., `let h = sin(1.5)`). |
| 21 | + |
| 22 | + Examples |
| 23 | + -------- |
| 24 | + > var y = sin( 0.0 ) |
| 25 | + 0.0 |
| 26 | + |
| 27 | + > y = sin( {{alias:@stdlib/constants/float64/pi}} / 2.0 ) |
| 28 | + 1.0 |
| 29 | + |
| 30 | + > y = sin( -{{alias:@stdlib/constants/float64/pi}} / 6.0 ) |
| 31 | + -0.5 |
| 32 | + |
| 33 | + > y = sin( 0.7853981633974483 ) |
| 34 | + ~0.7071 |
| 35 | + |
| 36 | + > y = sin( {{alias:@stdlib/constants/float64/pi}} ) |
| 37 | + 0.0 |
| 38 | + |
| 39 | + > y = sin( 2 * {{alias:@stdlib/constants/float64/pi}} ) |
| 40 | + 0.0 |
| 41 | + |
| 42 | + > y = sin( {{alias:@stdlib/constants/float64/pi}} / 3.0 ) |
| 43 | + ~0.8660 |
| 44 | + |
| 45 | + > y = sin( -{{alias:@stdlib/constants/float64/pi}} ) |
| 46 | + 0.0 |
| 47 | + |
| 48 | + > y = sin( Infinity ) |
| 49 | + NaN |
| 50 | + |
| 51 | + > y = sin( NaN ) |
| 52 | + NaN |
| 53 | + |
| 54 | + > y = sin( 1e308 ) |
| 55 | + NaN |
| 56 | + |
| 57 | + > y = sin( -1e308 ) |
| 58 | + NaN |
| 59 | + |
| 60 | + > y = sin( 1e-308 ) |
| 61 | + ~1e-308 |
| 62 | + |
| 63 | + > y = sin( -0.0 ) |
| 64 | + -0.0 |
| 65 | + |
| 66 | + > y = sin( 3 * {{alias:@stdlib/constants/float64/pi}} / 2.0 ) |
| 67 | + -1.0 |
| 68 | + |
| 69 | + > // Simulate a fading light effect: |
| 70 | + > for (let time = 0; time < 5; time += 0.5) { |
| 71 | + > let glow = (sin(time) + 1) / 2; |
| 72 | + > console.log(`Glow ${time}: ${glow.toFixed(2)}`); |
| 73 | + > } |
| 74 | + Glow 0: 0.50 |
| 75 | + Glow 0.5: 0.73 |
| 76 | + Glow 1: 0.91 |
| 77 | + ... |
| 78 | + |
| 79 | + > // Mix two sine waves: |
| 80 | + > let mix = sin(1.5) + sin(2.5); |
| 81 | + > mix |
| 82 | + ~-0.589 |
| 83 | + |
| 84 | + > // C example to simulate a bouncing ball: |
| 85 | + > // Save as `bounce.c` and compile with `gcc bounce.c -lm` |
| 86 | + > #include <math.h> |
| 87 | + > #include <stdio.h> |
| 88 | + > int main() { |
| 89 | + > for (int step = 0; step < 5; step++) { |
| 90 | + > printf("Height %d: %.2f\n", step, sin(step * 0.5)); |
| 91 | + > } |
| 92 | + > return 0; |
| 93 | + > } |
| 94 | + Height 0: 0.00 |
| 95 | + Height 1: 0.48 |
| 96 | + Height 2: 0.91 |
| 97 | + ... |
| 98 | + |
| 99 | + See Also |
| 100 | + -------- |
| 101 | + cos( x ): Computes the cosine, a related trigonometric function. |
| 102 | + tan( x ): Computes the tangent, useful for slopes. |
| 103 | + {{alias:@stdlib/constants/float64/pi}}: The constant π (approximately 3.14159). |
0 commit comments