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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//! # Sqr Function
//!
//! Returns a Double specifying the square root of a number.
//!
//! ## Syntax
//!
//! ```vb
//! Sqr(number)
//! ```
//!
//! ## Parameters
//!
//! - `number` - Required. Double or any valid numeric expression greater than or equal to 0.
//!
//! ## Return Value
//!
//! Returns a Double containing the square root of the number.
//!
//! ## Remarks
//!
//! The Sqr function calculates the positive square root of a non-negative number. It's one of the fundamental mathematical functions in VB6 and is commonly used in geometric calculations, statistical analysis, and scientific applications.
//!
//! Key characteristics:
//! - Returns the positive (principal) square root only
//! - Argument must be >= 0 (negative values cause Error 5)
//! - Returns Double for maximum precision
//! - Sqr(0) = 0
//! - Sqr(1) = 1
//! - Sqr(x) * Sqr(x) = x (within floating-point precision)
//! - Inverse operation of squaring: Sqr(x^2) = x (for x >= 0)
//!
//! Mathematical relationships:
//! - Sqr(x * y) = Sqr(x) * Sqr(y)
//! - Sqr(x / y) = Sqr(x) / Sqr(y)
//! - Sqr(x^2) = Abs(x)
//! - x^(1/2) = Sqr(x)
//! - x^(1/n) can be calculated as x^(1/n) = Exp(Log(x) / n)
//!
//! ## Typical Uses
//!
//! 1. **Distance Calculations**: Calculate distance between two points
//! 2. **Pythagorean Theorem**: Find hypotenuse or sides of right triangles
//! 3. **Standard Deviation**: Statistical calculations
//! 4. **Quadratic Equations**: Solve equations using quadratic formula
//! 5. **Normalization**: Normalize vectors and values
//! 6. **Root Mean Square**: Calculate RMS values
//! 7. **Geometric Calculations**: Circle, sphere, and other shape calculations
//! 8. **Physics Simulations**: Velocity, acceleration, and energy calculations
//!
//! ## Basic Examples
//!
//! ```vb
//! ' Example 1: Calculate square root of a number
//! Dim result As Double
//! result = Sqr(25)
//! ' Returns 5
//! ```
//!
//! ```vb
//! ' Example 2: Distance between two points (Pythagorean theorem)
//! Dim x1 As Double, y1 As Double
//! Dim x2 As Double, y2 As Double
//! Dim distance As Double
//!
//! x1 = 0: y1 = 0
//! x2 = 3: y2 = 4
//! distance = Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
//! ' Returns 5
//! ```
//!
//! ```vb
//! ' Example 3: Calculate hypotenuse of right triangle
//! Dim sideA As Double
//! Dim sideB As Double
//! Dim hypotenuse As Double
//!
//! sideA = 3
//! sideB = 4
//! hypotenuse = Sqr(sideA ^ 2 + sideB ^ 2)
//! ' Returns 5
//! ```
//!
//! ```vb
//! ' Example 4: Quadratic formula
//! Dim a As Double, b As Double, c As Double
//! Dim discriminant As Double
//! Dim root1 As Double, root2 As Double
//!
//! a = 1: b = -5: c = 6
//! discriminant = b ^ 2 - 4 * a * c
//!
//! If discriminant >= 0 Then
//! root1 = (-b + Sqr(discriminant)) / (2 * a)
//! root2 = (-b - Sqr(discriminant)) / (2 * a)
//! End If
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: `CalculateDistance2D`
//! Calculate distance between two 2D points
//! ```vb
//! Function CalculateDistance2D(x1 As Double, y1 As Double, _
//! x2 As Double, y2 As Double) As Double
//! Dim dx As Double
//! Dim dy As Double
//!
//! dx = x2 - x1
//! dy = y2 - y1
//!
//! CalculateDistance2D = Sqr(dx * dx + dy * dy)
//! End Function
//! ```
//!
//! ### Pattern 2: `CalculateDistance3D`
//! Calculate distance between two 3D points
//! ```vb
//! Function CalculateDistance3D(x1 As Double, y1 As Double, z1 As Double, _
//! x2 As Double, y2 As Double, z2 As Double) As Double
//! Dim dx As Double, dy As Double, dz As Double
//!
//! dx = x2 - x1
//! dy = y2 - y1
//! dz = z2 - z1
//!
//! CalculateDistance3D = Sqr(dx * dx + dy * dy + dz * dz)
//! End Function
//! ```
//!
//! ### Pattern 3: `CalculateHypotenuse`
//! Calculate hypotenuse of right triangle
//! ```vb
//! Function CalculateHypotenuse(sideA As Double, sideB As Double) As Double
//! CalculateHypotenuse = Sqr(sideA ^ 2 + sideB ^ 2)
//! End Function
//! ```
//!
//! ### Pattern 4: `CalculateStandardDeviation`
//! Calculate standard deviation of values
//! ```vb
//! Function CalculateStandardDeviation(values() As Double) As Double
//! Dim i As Integer
//! Dim sum As Double
//! Dim mean As Double
//! Dim sumSquaredDiff As Double
//! Dim count As Integer
//!
//! count = UBound(values) - LBound(values) + 1
//!
//! ' Calculate mean
//! sum = 0
//! For i = LBound(values) To UBound(values)
//! sum = sum + values(i)
//! Next i
//! mean = sum / count
//!
//! ' Calculate sum of squared differences
//! sumSquaredDiff = 0
//! For i = LBound(values) To UBound(values)
//! sumSquaredDiff = sumSquaredDiff + (values(i) - mean) ^ 2
//! Next i
//!
//! CalculateStandardDeviation = Sqr(sumSquaredDiff / count)
//! End Function
//! ```
//!
//! ### Pattern 5: `NormalizeVector`
//! Normalize a 2D vector
//! ```vb
//! Sub NormalizeVector(x As Double, y As Double)
//! Dim magnitude As Double
//!
//! magnitude = Sqr(x * x + y * y)
//!
//! If magnitude > 0 Then
//! x = x / magnitude
//! y = y / magnitude
//! End If
//! End Sub
//! ```
//!
//! ### Pattern 6: `CalculateRMS`
//! Calculate root mean square
//! ```vb
//! Function CalculateRMS(values() As Double) As Double
//! Dim i As Integer
//! Dim sumSquares As Double
//! Dim count As Integer
//!
//! count = UBound(values) - LBound(values) + 1
//! sumSquares = 0
//!
//! For i = LBound(values) To UBound(values)
//! sumSquares = sumSquares + values(i) ^ 2
//! Next i
//!
//! CalculateRMS = Sqr(sumSquares / count)
//! End Function
//! ```
//!
//! ### Pattern 7: `SolveQuadratic`
//! Solve quadratic equation ax² + bx + c = 0
//! ```vb
//! Function SolveQuadratic(a As Double, b As Double, c As Double, _
//! root1 As Double, root2 As Double) As Boolean
//! Dim discriminant As Double
//!
//! discriminant = b * b - 4 * a * c
//!
//! If discriminant < 0 Then
//! SolveQuadratic = False
//! Exit Function
//! End If
//!
//! root1 = (-b + Sqr(discriminant)) / (2 * a)
//! root2 = (-b - Sqr(discriminant)) / (2 * a)
//! SolveQuadratic = True
//! End Function
//! ```
//!
//! ### Pattern 8: `CalculateCircleRadius`
//! Calculate radius from area
//! ```vb
//! Function CalculateCircleRadius(area As Double) As Double
//! Const PI As Double = 3.14159265358979
//! CalculateCircleRadius = Sqr(area / PI)
//! End Function
//! ```
//!
//! ### Pattern 9: `CalculateVelocity`
//! Calculate velocity from kinetic energy
//! ```vb
//! Function CalculateVelocity(kineticEnergy As Double, mass As Double) As Double
//! ' KE = 1/2 * m * v^2
//! ' v = Sqr(2 * KE / m)
//! If mass > 0 Then
//! CalculateVelocity = Sqr(2 * kineticEnergy / mass)
//! Else
//! CalculateVelocity = 0
//! End If
//! End Function
//! ```
//!
//! ### Pattern 10: `IsPerfectSquare`
//! Check if number is a perfect square
//! ```vb
//! Function IsPerfectSquare(n As Long) As Boolean
//! Dim sqrtValue As Double
//! Dim intValue As Long
//!
//! If n < 0 Then
//! IsPerfectSquare = False
//! Exit Function
//! End If
//!
//! sqrtValue = Sqr(n)
//! intValue = CLng(sqrtValue)
//! IsPerfectSquare = (intValue * intValue = n)
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: `GeometryHelper` Class
//! Geometric calculations using Sqr
//! ```vb
//! ' Class: GeometryHelper
//!
//! Public Function DistanceBetweenPoints(x1 As Double, y1 As Double, _
//! x2 As Double, y2 As Double) As Double
//! DistanceBetweenPoints = Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
//! End Function
//!
//! Public Function PointToLineDistance(px As Double, py As Double, _
//! x1 As Double, y1 As Double, _
//! x2 As Double, y2 As Double) As Double
//! ' Distance from point to line segment
//! Dim A As Double, B As Double, C As Double
//!
//! A = px - x1
//! B = py - y1
//! C = x2 - x1
//! Dim D As Double
//! D = y2 - y1
//!
//! Dim dot As Double
//! dot = A * C + B * D
//! Dim lenSq As Double
//! lenSq = C * C + D * D
//!
//! Dim param As Double
//! param = -1
//! If lenSq <> 0 Then param = dot / lenSq
//!
//! Dim xx As Double, yy As Double
//!
//! If param < 0 Then
//! xx = x1
//! yy = y1
//! ElseIf param > 1 Then
//! xx = x2
//! yy = y2
//! Else
//! xx = x1 + param * C
//! yy = y1 + param * D
//! End If
//!
//! Dim dx As Double, dy As Double
//! dx = px - xx
//! dy = py - yy
//!
//! PointToLineDistance = Sqr(dx * dx + dy * dy)
//! End Function
//!
//! Public Function CircleArea(radius As Double) As Double
//! Const PI As Double = 3.14159265358979
//! CircleArea = PI * radius * radius
//! End Function
//!
//! Public Function CircleRadiusFromArea(area As Double) As Double
//! Const PI As Double = 3.14159265358979
//! CircleRadiusFromArea = Sqr(area / PI)
//! End Function
//!
//! Public Function SphereVolume(radius As Double) As Double
//! Const PI As Double = 3.14159265358979
//! SphereVolume = (4# / 3#) * PI * radius ^ 3
//! End Function
//!
//! Public Function SphereRadiusFromVolume(volume As Double) As Double
//! Const PI As Double = 3.14159265358979
//! SphereRadiusFromVolume = ((3 * volume) / (4 * PI)) ^ (1# / 3#)
//! End Function
//! ```
//!
//! ### Example 2: `StatisticsCalculator` Class
//! Statistical calculations with Sqr
//! ```vb
//! ' Class: StatisticsCalculator
//! Private m_values() As Double
//! Private m_count As Integer
//!
//! Public Sub AddValue(value As Double)
//! If m_count > UBound(m_values) Then
//! ReDim Preserve m_values(0 To m_count * 2)
//! End If
//! m_values(m_count) = value
//! m_count = m_count + 1
//! End Sub
//!
//! Public Sub Clear()
//! m_count = 0
//! ReDim m_values(0 To 9)
//! End Sub
//!
//! Private Sub Class_Initialize()
//! Clear
//! End Sub
//!
//! Public Function GetMean() As Double
//! Dim sum As Double
//! Dim i As Integer
//!
//! If m_count = 0 Then
//! GetMean = 0
//! Exit Function
//! End If
//!
//! sum = 0
//! For i = 0 To m_count - 1
//! sum = sum + m_values(i)
//! Next i
//!
//! GetMean = sum / m_count
//! End Function
//!
//! Public Function GetStandardDeviation() As Double
//! Dim mean As Double
//! Dim sumSquaredDiff As Double
//! Dim i As Integer
//!
//! If m_count = 0 Then
//! GetStandardDeviation = 0
//! Exit Function
//! End If
//!
//! mean = GetMean()
//! sumSquaredDiff = 0
//!
//! For i = 0 To m_count - 1
//! sumSquaredDiff = sumSquaredDiff + (m_values(i) - mean) ^ 2
//! Next i
//!
//! GetStandardDeviation = Sqr(sumSquaredDiff / m_count)
//! End Function
//!
//! Public Function GetVariance() As Double
//! Dim sd As Double
//! sd = GetStandardDeviation()
//! GetVariance = sd * sd
//! End Function
//!
//! Public Function GetRMS() As Double
//! Dim sumSquares As Double
//! Dim i As Integer
//!
//! If m_count = 0 Then
//! GetRMS = 0
//! Exit Function
//! End If
//!
//! sumSquares = 0
//! For i = 0 To m_count - 1
//! sumSquares = sumSquares + m_values(i) ^ 2
//! Next i
//!
//! GetRMS = Sqr(sumSquares / m_count)
//! End Function
//!
//! Public Property Get Count() As Integer
//! Count = m_count
//! End Property
//! ```
//!
//! ### Example 3: `PhysicsEngine` Module
//! Physics calculations using Sqr
//! ```vb
//! ' Module: PhysicsEngine
//!
//! Public Function CalculateVelocityFromEnergy(kineticEnergy As Double, _
//! mass As Double) As Double
//! ' v = Sqr(2 * KE / m)
//! If mass > 0 Then
//! CalculateVelocityFromEnergy = Sqr(2 * kineticEnergy / mass)
//! Else
//! CalculateVelocityFromEnergy = 0
//! End If
//! End Function
//!
//! Public Function CalculateEscapeVelocity(mass As Double, radius As Double) As Double
//! ' v_escape = Sqr(2 * G * M / R)
//! Const G As Double = 6.674E-11 ' Gravitational constant
//! CalculateEscapeVelocity = Sqr(2 * G * mass / radius)
//! End Function
//!
//! Public Function CalculatePeriod(length As Double) As Double
//! ' Period of simple pendulum: T = 2Ï€ * Sqr(L/g)
//! Const PI As Double = 3.14159265358979
//! Const g As Double = 9.81 ' Gravity
//! CalculatePeriod = 2 * PI * Sqr(length / g)
//! End Function
//!
//! Public Function CalculateFallTime(height As Double) As Double
//! ' Time to fall from height: t = Sqr(2h/g)
//! Const g As Double = 9.81 ' Gravity
//! CalculateFallTime = Sqr(2 * height / g)
//! End Function
//!
//! Public Function CalculateImpactVelocity(height As Double) As Double
//! ' v = Sqr(2gh)
//! Const g As Double = 9.81 ' Gravity
//! CalculateImpactVelocity = Sqr(2 * g * height)
//! End Function
//!
//! Public Function CalculateOrbitalVelocity(mass As Double, radius As Double) As Double
//! ' v = Sqr(G * M / R)
//! Const G As Double = 6.674E-11 ' Gravitational constant
//! CalculateOrbitalVelocity = Sqr(G * mass / radius)
//! End Function
//! ```
//!
//! ### Example 4: `VectorMath` Module
//! Vector operations using Sqr
//! ```vb
//! ' Module: VectorMath
//!
//! Public Function VectorMagnitude2D(x As Double, y As Double) As Double
//! VectorMagnitude2D = Sqr(x * x + y * y)
//! End Function
//!
//! Public Function VectorMagnitude3D(x As Double, y As Double, z As Double) As Double
//! VectorMagnitude3D = Sqr(x * x + y * y + z * z)
//! End Function
//!
//! Public Sub NormalizeVector2D(x As Double, y As Double)
//! Dim magnitude As Double
//! magnitude = VectorMagnitude2D(x, y)
//!
//! If magnitude > 0 Then
//! x = x / magnitude
//! y = y / magnitude
//! End If
//! End Sub
//!
//! Public Sub NormalizeVector3D(x As Double, y As Double, z As Double)
//! Dim magnitude As Double
//! magnitude = VectorMagnitude3D(x, y, z)
//!
//! If magnitude > 0 Then
//! x = x / magnitude
//! y = y / magnitude
//! z = z / magnitude
//! End If
//! End Sub
//!
//! Public Function DotProduct2D(x1 As Double, y1 As Double, _
//! x2 As Double, y2 As Double) As Double
//! DotProduct2D = x1 * x2 + y1 * y2
//! End Function
//!
//! Public Function VectorDistance2D(x1 As Double, y1 As Double, _
//! x2 As Double, y2 As Double) As Double
//! VectorDistance2D = Sqr((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
//! End Function
//!
//! Public Function VectorAngleBetween(x1 As Double, y1 As Double, _
//! x2 As Double, y2 As Double) As Double
//! ' Returns angle in radians
//! Dim dot As Double
//! Dim mag1 As Double, mag2 As Double
//!
//! dot = DotProduct2D(x1, y1, x2, y2)
//! mag1 = VectorMagnitude2D(x1, y1)
//! mag2 = VectorMagnitude2D(x2, y2)
//!
//! If mag1 > 0 And mag2 > 0 Then
//! VectorAngleBetween = Atn(Sqr(1 - (dot / (mag1 * mag2)) ^ 2) / (dot / (mag1 * mag2)))
//! Else
//! VectorAngleBetween = 0
//! End If
//! End Function
//! ```
//!
//! ## Error Handling
//!
//! The Sqr function can generate the following errors:
//!
//! - **Error 5** (Invalid procedure call or argument): If number is negative
//! - **Error 13** (Type mismatch): If argument is not numeric
//!
//! Always validate inputs:
//! ```vb
//! On Error Resume Next
//! If value >= 0 Then
//! result = Sqr(value)
//! If Err.Number <> 0 Then
//! MsgBox "Error calculating square root: " & Err.Description
//! End If
//! Else
//! MsgBox "Cannot calculate square root of negative number"
//! End If
//! ```
//!
//! ## Performance Considerations
//!
//! - Sqr is a relatively fast operation
//! - Uses hardware floating-point unit when available
//! - For repeated calculations, consider caching results
//! - Sqr(x^2) is slower than Abs(x) for getting absolute value
//! - For integer square roots, consider using Int(Sqr(x))
//!
//! ## Best Practices
//!
//! 1. **Validate Input**: Always ensure argument is non-negative
//! 2. **Use for Distances**: Ideal for Euclidean distance calculations
//! 3. **Combine with ^2**: Use for Pythagorean theorem applications
//! 4. **Handle Zero**: Remember Sqr(0) = 0 is valid
//! 5. **Precision Aware**: Understand floating-point precision limits
//! 6. **Error Handling**: Trap errors for negative inputs
//! 7. **Performance**: Cache results when used repeatedly
//! 8. **Consider Abs**: For Sqr(x^2), use Abs(x) instead
//! 9. **Document Units**: Comment on units in physics calculations
//! 10. **Test Edge Cases**: Test with 0, very small, and very large values
//!
//! ## Comparison with Related Functions
//!
//! | Operation | VB6 Function | Example | Result |
//! |-----------|--------------|---------|--------|
//! | Square root | Sqr(x) | Sqr(25) | 5 |
//! | Cube root | x^(1/3) | 27^(1/3) | 3 |
//! | Nth root | x^(1/n) | 16^(1/4) | 2 |
//! | Power | x^y | 2^3 | 8 |
//! | Absolute value | Abs(x) | Abs(-5) | 5 |
//!
//! ## Platform Considerations
//!
//! - Available in VB6, VBA (all versions)
//! - Part of core mathematical functions
//! - Uses IEEE 754 floating-point arithmetic
//! - Consistent behavior across platforms
//! - Hardware-accelerated on modern processors
//!
//! ## Limitations
//!
//! - Cannot calculate square root of negative numbers (use complex number libraries)
//! - Subject to floating-point precision limits (~15-17 significant digits)
//! - Sqr(x)^2 may not exactly equal x due to rounding
//! - Very large numbers may overflow Double range
//! - Very small numbers may underflow to zero
//!
//! ## Related Functions
//!
//! - `Abs`: Returns absolute value of a number
//! - `Exp`: Returns e raised to a power
//! - `Log`: Returns natural logarithm
//! - `^` operator: Raises number to a power (x^0.5 = Sqr(x))
//!
use crate::;
/// Implementation of the square root (Sqr) function.
///
/// VB6 behavior:
/// - `Sqr(Null)` returns `Null`
/// - other values are coerced with numeric conversion rules and return `Double`