Skip to content

Commit 7926ec6

Browse files
committed
docs: enhance TypeScript declaration for sin() with input constraints and more features
1 parent 909a027 commit 7926ec6

File tree

1 file changed

+109
-7
lines changed
  • lib/node_modules/@stdlib/math/base/special/sin/docs/types

1 file changed

+109
-7
lines changed

lib/node_modules/@stdlib/math/base/special/sin/docs/types/index.d.ts

Lines changed: 109 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,130 @@
2121
/**
2222
* Computes the sine of a number.
2323
*
24+
* ## Notes
25+
* - The input value `x` must be in radians. To convert degrees to radians, multiply by `π/180` (e.g., `sin(45 * Math.PI / 180)`).
26+
* - The input `x` should be a finite number for meaningful results. Non-finite inputs like `Infinity`, `-Infinity`, or `NaN` return `NaN`.
27+
* - For very large finite inputs (e.g., `|x| > 1e308`), the result may lose precision due to the periodic nature of sine and floating-point limitations.
28+
* - The function returns a value between -1 and 1.
29+
* - The sine function is periodic with a period of 2π (approximately 6.28 radians), so `sin(x) = sin(x % (2 * Math.PI))`.
30+
* - The function satisfies the symmetry property `sin(-x) = -sin(x)`.
31+
* - For very small inputs (`|x| < 1e-308`), `sin(x) ≈ x` due to the small-angle approximation.
32+
* - The function preserves the sign of zero: `sin(-0.0)` returns `-0.0`.
33+
*
2434
* @param x - input value (in radians)
2535
* @returns sine
2636
*
2737
* @example
38+
* // Basic example: sine of 0 radians (0 degrees)
2839
* var v = sin( 0.0 );
29-
* // returns ~0.0
40+
* // returns 0.0
41+
*
42+
* @example
43+
* // Sine of π/2 radians (90 degrees), where sine reaches its maximum
44+
* var v = sin( 3.141592653589793 / 2.0 );
45+
* // returns 1.0
46+
*
47+
* @example
48+
* // Sine of -π/6 radians (-30 degrees), a negative angle
49+
* var v = sin( -3.141592653589793 / 6.0 );
50+
* // returns -0.5
51+
*
52+
* @example
53+
* // Sine of π radians (180 degrees), a full half-circle
54+
* var v = sin( 3.141592653589793 );
55+
* // returns 0.0
56+
*
57+
* @example
58+
* // Sine of π/4 radians (45 degrees), a common angle where sin(π/4) = √2/2
59+
* var v = sin( 3.141592653589793 / 4.0 );
60+
* // returns ~0.7071 (approximately √2/2)
61+
*
62+
* @example
63+
* // Sine of π/3 radians (60 degrees), another common angle where sin(π/3) = √3/2
64+
* var v = sin( 3.141592653589793 / 3.0 );
65+
* // returns ~0.8660 (approximately √3/2)
66+
*
67+
* @example
68+
* // Sine of 3π/2 radians (270 degrees), where sine reaches its minimum
69+
* var v = sin( 3 * 3.141592653589793 / 2.0 );
70+
* // returns -1.0
3071
*
3172
* @example
32-
* var v = sin( 3.141592653589793/2.0 );
33-
* // returns ~1.0
73+
* // Edge case: sine of negative zero, should preserve the sign
74+
* var v = sin( -0.0 );
75+
* // returns -0.0
3476
*
3577
* @example
36-
* var v = sin( -3.141592653589793/6.0 );
37-
* // returns ~-0.5
78+
* // Edge case: sine of a very small number, where sin(x) ≈ x
79+
* var v = sin( 1e-308 );
80+
* // returns ~1e-308
3881
*
3982
* @example
83+
* // Edge case: sine of a very large number, exceeds safe computation range
84+
* var v = sin( 1e308 );
85+
* // returns NaN
86+
*
87+
* @example
88+
* // Edge case: sine of negative infinity, non-finite input
89+
* var v = sin( -Infinity );
90+
* // returns NaN
91+
*
92+
* @example
93+
* // Edge case: sine of NaN, invalid input
4094
* var v = sin( NaN );
4195
* // returns NaN
96+
*
97+
* @example
98+
* // Practical use: animate a pulsating effect (e.g., for a UI element)
99+
* function pulsate(time: number): number {
100+
* return 0.5 + 0.5 * sin(time); // Oscillates between 0 and 1
101+
* }
102+
* for (let t = 0; t < 5; t += 0.5) {
103+
* console.log(`Time: ${t.toFixed(1)}, Pulse: ${pulsate(t).toFixed(3)}`);
104+
* }
105+
* // Outputs: Time: 0.0, Pulse: 0.500; Time: 0.5, Pulse: 0.739; etc.
106+
*
107+
* @example
108+
* // Practical use: generate a wave pattern with multiple frequencies (e.g., for audio synthesis)
109+
* function generateWave(time: number, freq1: number, freq2: number): number {
110+
* // Combine two sine waves with different frequencies
111+
* const wave1 = sin(freq1 * time); // First frequency component
112+
* const wave2 = sin(freq2 * time); // Second frequency component
113+
* return (wave1 + wave2) / 2; // Average the waves to keep amplitude between -1 and 1
114+
* }
115+
* for (let t = 0; t < 5; t += 0.5) {
116+
* console.log(`Time: ${t.toFixed(1)}, Wave: ${generateWave(t, 1.0, 2.0).toFixed(3)}`);
117+
* }
118+
* // Outputs: Time: 0.0, Wave: 0.000; Time: 0.5, Wave: 0.609; etc.
119+
*
120+
* @example
121+
* // Test cases to verify behavior
122+
* function testSin(): void {
123+
* // Test basic values
124+
* if (sin(0.0) !== 0.0) {
125+
* throw new Error('Expected sin(0.0) to be 0.0');
126+
* }
127+
* if (sin(3.141592653589793 / 2.0) !== 1.0) {
128+
* throw new Error('Expected sin(π/2) to be 1.0');
129+
* }
130+
* if (sin(3.141592653589793) !== 0.0) {
131+
* throw new Error('Expected sin(π) to be 0.0');
132+
* }
133+
* // Test common angle
134+
* if (Math.abs(sin(3.141592653589793 / 4.0) - 0.7071067811865476) > 1e-10) {
135+
* throw new Error('Expected sin(π/4) to be approximately 0.7071067811865476');
136+
* }
137+
* // Test edge cases
138+
* if (sin(-0.0) !== -0.0) {
139+
* throw new Error('Expected sin(-0.0) to be -0.0');
140+
* }
141+
* if (isNaN(sin(1e308)) === false) {
142+
* throw new Error('Expected sin(1e308) to be NaN');
143+
* }
144+
* }
42145
*/
43146
declare function sin( x: number ): number;
44147

45-
46148
// EXPORTS //
47149

48-
export = sin;
150+
export = sin;

0 commit comments

Comments
 (0)