1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! # Cos Function
//!
//! Returns a Double specifying the cosine of an angle.
//!
//! ## Syntax
//!
//! ```vb
//! Cos(number)
//! ```
//!
//! ## Parameters
//!
//! - **number**: Required. Double or any valid numeric expression that expresses an angle in radians.
//!
//! ## Return Value
//!
//! Returns a Double representing the cosine of the angle. The return value ranges from -1 to 1.
//!
//! ## Remarks
//!
//! The `Cos` function takes an angle in radians and returns the ratio of the length of the
//! adjacent side to the length of the hypotenuse in a right triangle. This is a fundamental
//! trigonometric function used in mathematical calculations, graphics programming, physics
//! simulations, and engineering applications.
//!
//! **Important Characteristics:**
//!
//! - Argument is in radians, not degrees
//! - Return value range: -1 to 1
//! - Cos(0) = 1
//! - Cos(π/2) ≈ 0
//! - Cos(π) = -1
//! - Cos(3π/2) ≈ 0
//! - Cos(2π) = 1
//! - Periodic with period 2π
//! - Even function: Cos(-x) = Cos(x)
//!
//! ## Angle Conversion
//!
//! To convert degrees to radians (required for Cos):
//! ```vb
//! radians = degrees * (π / 180)
//! radians = degrees * 0.0174532925199433
//! ```
//!
//! To convert radians to degrees:
//! ```vb
//! degrees = radians * (180 / π)
//! degrees = radians * 57.2957795130823
//! ```
//!
//! ## Common Angle Values
//!
//! | Degrees | Radians | Cos(angle) |
//! |---------|---------|------------|
//! | 0° | 0 | 1 |
//! | 30° | π/6 | √3/2 ≈ 0.866 |
//! | 45° | π/4 | √2/2 ≈ 0.707 |
//! | 60° | π/3 | 0.5 |
//! | 90° | π/2 | 0 |
//! | 120° | 2π/3 | -0.5 |
//! | 135° | 3π/4 | -√2/2 ≈ -0.707 |
//! | 150° | 5π/6 | -√3/2 ≈ -0.866 |
//! | 180° | π | -1 |
//! | 270° | 3π/2 | 0 |
//! | 360° | 2π | 1 |
//!
//! ## Examples
//!
//! ### Basic Usage
//!
//! ```vb
//! ' Calculate cosine of an angle in radians
//! Dim result As Double
//! result = Cos(0) ' Returns 1
//! result = Cos(1.5708) ' Returns approximately 0 (π/2)
//! result = Cos(3.14159) ' Returns approximately -1 (π)
//!
//! ' Using with degrees (convert first)
//! Dim angleInDegrees As Double
//! Dim angleInRadians As Double
//! angleInDegrees = 45
//! angleInRadians = angleInDegrees * (3.14159265358979 / 180)
//! result = Cos(angleInRadians) ' Returns approximately 0.707
//! ```
//!
//! ### Using Pi Constant
//!
//! ```vb
//! Const Pi As Double = 3.14159265358979
//!
//! Dim angle As Double
//! angle = Cos(Pi / 4) ' 45 degrees, returns ≈0.707
//! angle = Cos(Pi / 2) ' 90 degrees, returns ≈0
//! angle = Cos(Pi) ' 180 degrees, returns -1
//! angle = Cos(2 * Pi) ' 360 degrees, returns 1
//! ```
//!
//! ### Degrees to Radians Conversion
//!
//! ```vb
//! Function DegreesToRadians(degrees As Double) As Double
//! Const Pi As Double = 3.14159265358979
//! DegreesToRadians = degrees * (Pi / 180)
//! End Function
//!
//! Function CosDegrees(degrees As Double) As Double
//! CosDegrees = Cos(DegreesToRadians(degrees))
//! End Function
//!
//! ' Usage
//! Dim result As Double
//! result = CosDegrees(60) ' Returns 0.5
//! ```
//!
//! ## Common Patterns
//!
//! ### Circle Point Calculation
//!
//! ```vb
//! Function GetCirclePoint(centerX As Double, centerY As Double, _
//! radius As Double, angleDegrees As Double) As Point
//! Const Pi As Double = 3.14159265358979
//! Dim angleRad As Double
//! Dim pt As Point
//!
//! angleRad = angleDegrees * (Pi / 180)
//!
//! pt.X = centerX + radius * Cos(angleRad)
//! pt.Y = centerY + radius * Sin(angleRad)
//!
//! GetCirclePoint = pt
//! End Function
//! ```
//!
//! ### Rotating Points
//!
//! ```vb
//! Function RotatePoint(x As Double, y As Double, angleDegrees As Double) As Point
//! Const Pi As Double = 3.14159265358979
//! Dim angleRad As Double
//! Dim pt As Point
//!
//! angleRad = angleDegrees * (Pi / 180)
//!
//! pt.X = x * Cos(angleRad) - y * Sin(angleRad)
//! pt.Y = x * Sin(angleRad) + y * Cos(angleRad)
//!
//! RotatePoint = pt
//! End Function
//! ```
//!
//! ### Wave Generation
//!
//! ```vb
//! Function GenerateCosineWave(samples As Integer, amplitude As Double, _
//! frequency As Double) As Double()
//! Const Pi As Double = 3.14159265358979
//! Dim wave() As Double
//! Dim i As Integer
//! Dim angle As Double
//!
//! ReDim wave(0 To samples - 1)
//!
//! For i = 0 To samples - 1
//! angle = 2 * Pi * frequency * i / samples
//! wave(i) = amplitude * Cos(angle)
//! Next i
//!
//! GenerateCosineWave = wave
//! End Function
//! ```
//!
//! ### Harmonic Motion
//!
//! ```vb
//! Function SimpleHarmonicMotion(amplitude As Double, frequency As Double, _
//! time As Double) As Double
//! Const Pi As Double = 3.14159265358979
//! SimpleHarmonicMotion = amplitude * Cos(2 * Pi * frequency * time)
//! End Function
//! ```
//!
//! ### Distance Calculation
//!
//! ```vb
//! Function DistanceToLine(px As Double, py As Double, _
//! angle As Double, distance As Double) As Point
//! Const Pi As Double = 3.14159265358979
//! Dim pt As Point
//! Dim angleRad As Double
//!
//! angleRad = angle * (Pi / 180)
//!
//! pt.X = px + distance * Cos(angleRad)
//! pt.Y = py + distance * Sin(angleRad)
//!
//! DistanceToLine = pt
//! End Function
//! ```
//!
//! ### Ellipse Points
//!
//! ```vb
//! Function GetEllipsePoint(centerX As Double, centerY As Double, _
//! radiusX As Double, radiusY As Double, _
//! angleDegrees As Double) As Point
//! Const Pi As Double = 3.14159265358979
//! Dim angleRad As Double
//! Dim pt As Point
//!
//! angleRad = angleDegrees * (Pi / 180)
//!
//! pt.X = centerX + radiusX * Cos(angleRad)
//! pt.Y = centerY + radiusY * Sin(angleRad)
//!
//! GetEllipsePoint = pt
//! End Function
//! ```
//!
//! ### Polar to Cartesian Conversion
//!
//! ```vb
//! Function PolarToCartesian(radius As Double, angleDegrees As Double) As Point
//! Const Pi As Double = 3.14159265358979
//! Dim angleRad As Double
//! Dim pt As Point
//!
//! angleRad = angleDegrees * (Pi / 180)
//!
//! pt.X = radius * Cos(angleRad)
//! pt.Y = radius * Sin(angleRad)
//!
//! PolarToCartesian = pt
//! End Function
//! ```
//!
//! ### Clock Hand Position
//!
//! ```vb
//! Function GetClockHandPosition(centerX As Double, centerY As Double, _
//! handLength As Double, hours As Integer, _
//! minutes As Integer) As Point
//! Const Pi As Double = 3.14159265358979
//! Dim angle As Double
//! Dim angleRad As Double
//! Dim pt As Point
//!
//! ' Calculate angle (12 o'clock = 0 degrees, clockwise)
//! angle = (hours Mod 12) * 30 + minutes * 0.5 ' 30 degrees per hour
//! angle = angle - 90 ' Adjust so 0 degrees is at 3 o'clock position
//!
//! angleRad = angle * (Pi / 180)
//!
//! pt.X = centerX + handLength * Cos(angleRad)
//! pt.Y = centerY + handLength * Sin(angleRad)
//!
//! GetClockHandPosition = pt
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### 3D Rotation (Yaw)
//!
//! ```vb
//! Function RotateYaw(x As Double, y As Double, z As Double, _
//! angleDegrees As Double) As Point3D
//! Const Pi As Double = 3.14159265358979
//! Dim angleRad As Double
//! Dim pt As Point3D
//!
//! angleRad = angleDegrees * (Pi / 180)
//!
//! pt.X = x * Cos(angleRad) - z * Sin(angleRad)
//! pt.Y = y
//! pt.Z = x * Sin(angleRad) + z * Cos(angleRad)
//!
//! RotateYaw = pt
//! End Function
//! ```
//!
//! ### Fourier Series
//!
//! ```vb
//! Function FourierCosine(x As Double, coefficients() As Double) As Double
//! Const Pi As Double = 3.14159265358979
//! Dim result As Double
//! Dim i As Integer
//!
//! result = coefficients(0) / 2 ' a0/2 term
//!
//! For i = 1 To UBound(coefficients)
//! result = result + coefficients(i) * Cos(i * x)
//! Next i
//!
//! FourierCosine = result
//! End Function
//! ```
//!
//! ### Dot Product Calculation
//!
//! ```vb
//! Function DotProduct(x1 As Double, y1 As Double, _
//! x2 As Double, y2 As Double) As Double
//! ' Alternative: using angle between vectors
//! Dim magnitude1 As Double, magnitude2 As Double
//! Dim angle As Double
//!
//! magnitude1 = Sqr(x1 * x1 + y1 * y1)
//! magnitude2 = Sqr(x2 * x2 + y2 * y2)
//!
//! ' Get angle between vectors using Atn2
//! angle = Atn2(y2, x2) - Atn2(y1, x1)
//!
//! DotProduct = magnitude1 * magnitude2 * Cos(angle)
//! End Function
//! ```
//!
//! ## Error Handling
//!
//! ```vb
//! Function SafeCos(angle As Double) As Double
//! On Error GoTo ErrorHandler
//!
//! SafeCos = Cos(angle)
//! Exit Function
//!
//! ErrorHandler:
//! ' Cos rarely throws errors, but handle overflow
//! MsgBox "Error calculating cosine: " & Err.Description
//! SafeCos = 0
//! End Function
//! ```
//!
//! ### Common Errors
//!
//! - **Error 6** (Overflow): Can occur with extremely large angle values
//! - **Error 13** (Type mismatch): Non-numeric argument
//!
//! ## Performance Considerations
//!
//! - `Cos` is a native function with good performance
//! - For repeated calculations with the same angles, cache results
//! - Consider lookup tables for fixed angle values in performance-critical code
//! - Reducing angle to [0, 2π] range before calling Cos can improve accuracy
//!
//! ## Mathematical Properties
//!
//! ### Pythagorean Identity
//!
//! ```vb
//! ' Cos²(x) + Sin²(x) = 1
//! Function VerifyPythagorean(angle As Double) As Boolean
//! Dim cosSquared As Double, sinSquared As Double
//! cosSquared = Cos(angle) ^ 2
//! sinSquared = Sin(angle) ^ 2
//! VerifyPythagorean = Abs((cosSquared + sinSquared) - 1) < 0.0000001
//! End Function
//! ```
//!
//! ### Even Function Property
//!
//! ```vb
//! ' Cos(-x) = Cos(x)
//! Function VerifyEvenProperty(angle As Double) As Boolean
//! VerifyEvenProperty = Abs(Cos(-angle) - Cos(angle)) < 0.0000001
//! End Function
//! ```
//!
//! ### Angle Addition Formula
//!
//! ```vb
//! ' Cos(a + b) = Cos(a)Cos(b) - Sin(a)Sin(b)
//! Function CosSum(angleA As Double, angleB As Double) As Double
//! CosSum = Cos(angleA) * Cos(angleB) - Sin(angleA) * Sin(angleB)
//! End Function
//! ```
//!
//! ## Limitations
//!
//! - Argument must be in radians (not degrees)
//! - Very large angles may lose precision due to floating-point limitations
//! - Return value is always between -1 and 1
//! - Small rounding errors may occur near critical angles (e.g., π/2)
//! - For angles outside normal range, consider normalizing to [0, 2π]
//!
//! ## Related Functions
//!
//! - `Sin`: Returns the sine of an angle (complementary to cosine)
//! - `Tan`: Returns the tangent of an angle (Sin/Cos)
//! - `Atn`: Returns the arctangent (inverse tangent)
//! - `Acos`: Arc cosine (inverse of cosine, not built-in VB6)
//! - `Sqr`: Square root function (useful for magnitude calculations)
use crate::;
/// Implementation of the cosine (Cos) function.
///
/// VB6 behavior:
/// - `Cos(Null)` returns `Null`
/// - other values are coerced with numeric conversion rules and return `Double`