<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="VB6Parse Library Reference - sqr - Math">
<title>sqr - Math - VB6Parse Library Reference</title>
<link rel="stylesheet" href="../../../assets/css/style.css">
<link rel="stylesheet" href="../../../assets/css/docs-style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
<script src="../../../assets/js/theme-switcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<header class="docs-header">
<div class="container">
<h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/math/index.html">Math</a> / sqr</h1>
<p class="tagline">VB6 Library Reference</p>
</div>
</header>
<nav class="docs-nav">
<div class="container">
<a href="../../../index.html">Home</a>
<a href="../../../library/index.html">Library Reference</a>
<a href="../../../documentation.html">Documentation</a>
<a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
<a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
<button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
<span class="theme-icon">🌙</span>
</button>
</div>
</nav>
<main class="container">
<article class="library-item">
<h1 id="sqr-function">Sqr Function</h1>
<p>Returns a Double specifying the square root of a number.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Sqr(number)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>number</code> - Required. Double or any valid numeric expression greater than or equal to 0.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a Double containing the square root of the number.</p>
<h2 id="remarks">Remarks</h2>
<p>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)</p>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Distance Calculations</strong>: Calculate distance between two points</li>
<li><strong>Pythagorean Theorem</strong>: Find hypotenuse or sides of right triangles</li>
<li><strong>Standard Deviation</strong>: Statistical calculations</li>
<li><strong>Quadratic Equations</strong>: Solve equations using quadratic formula</li>
<li><strong>Normalization</strong>: Normalize vectors and values</li>
<li><strong>Root Mean Square</strong>: Calculate RMS values</li>
<li><strong>Geometric Calculations</strong>: Circle, sphere, and other shape calculations</li>
<li><strong>Physics Simulations</strong>: Velocity, acceleration, and energy calculations</li>
</ol>
<h2 id="basic-examples">Basic Examples</h2>
<pre><code class="language-vbnet">' Example 1: Calculate square root of a number
Dim result As Double
result = Sqr(25)
' Returns 5</code></pre>
<pre><code class="language-vbnet">' 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</code></pre>
<pre><code class="language-vbnet">' 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</code></pre>
<pre><code class="language-vbnet">' 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</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="pattern-1-calculatedistance2d">Pattern 1: <code>CalculateDistance2D</code></h3>
<p>Calculate distance between two 2D points</p>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="pattern-2-calculatedistance3d">Pattern 2: <code>CalculateDistance3D</code></h3>
<p>Calculate distance between two 3D points</p>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="pattern-3-calculatehypotenuse">Pattern 3: <code>CalculateHypotenuse</code></h3>
<p>Calculate hypotenuse of right triangle</p>
<pre><code class="language-vbnet">Function CalculateHypotenuse(sideA As Double, sideB As Double) As Double
CalculateHypotenuse = Sqr(sideA ^ 2 + sideB ^ 2)
End Function</code></pre>
<h3 id="pattern-4-calculatestandarddeviation">Pattern 4: <code>CalculateStandardDeviation</code></h3>
<p>Calculate standard deviation of values</p>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="pattern-5-normalizevector">Pattern 5: <code>NormalizeVector</code></h3>
<p>Normalize a 2D vector</p>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="pattern-6-calculaterms">Pattern 6: <code>CalculateRMS</code></h3>
<p>Calculate root mean square</p>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="pattern-7-solvequadratic">Pattern 7: <code>SolveQuadratic</code></h3>
<p>Solve quadratic equation ax² + bx + c = 0</p>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="pattern-8-calculatecircleradius">Pattern 8: <code>CalculateCircleRadius</code></h3>
<p>Calculate radius from area</p>
<pre><code class="language-vbnet">Function CalculateCircleRadius(area As Double) As Double
Const PI As Double = 3.14159265358979
CalculateCircleRadius = Sqr(area / PI)
End Function</code></pre>
<h3 id="pattern-9-calculatevelocity">Pattern 9: <code>CalculateVelocity</code></h3>
<p>Calculate velocity from kinetic energy</p>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="pattern-10-isperfectsquare">Pattern 10: <code>IsPerfectSquare</code></h3>
<p>Check if number is a perfect square</p>
<pre><code class="language-vbnet">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</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="example-1-geometryhelper-class">Example 1: <code>GeometryHelper</code> Class</h3>
<p>Geometric calculations using Sqr</p>
<pre><code class="language-vbnet">' 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</code></pre>
<h3 id="example-2-statisticscalculator-class">Example 2: <code>StatisticsCalculator</code> Class</h3>
<p>Statistical calculations with Sqr</p>
<pre><code class="language-vbnet">' 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</code></pre>
<h3 id="example-3-physicsengine-module">Example 3: <code>PhysicsEngine</code> Module</h3>
<p>Physics calculations using Sqr</p>
<pre><code class="language-vbnet">' 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</code></pre>
<h3 id="example-4-vectormath-module">Example 4: <code>VectorMath</code> Module</h3>
<p>Vector operations using Sqr</p>
<pre><code class="language-vbnet">' 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</code></pre>
<h2 id="error-handling">Error Handling</h2>
<p>The Sqr function can generate the following errors:
- <strong>Error 5</strong> (Invalid procedure call or argument): If number is negative
- <strong>Error 13</strong> (Type mismatch): If argument is not numeric
Always validate inputs:</p>
<pre><code class="language-vbnet">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</code></pre>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li>Sqr is a relatively fast operation</li>
<li>Uses hardware floating-point unit when available</li>
<li>For repeated calculations, consider caching results</li>
<li>Sqr(x^2) is slower than Abs(x) for getting absolute value</li>
<li>For integer square roots, consider using Int(Sqr(x))</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Validate Input</strong>: Always ensure argument is non-negative</li>
<li><strong>Use for Distances</strong>: Ideal for Euclidean distance calculations</li>
<li><strong>Combine with ^2</strong>: Use for Pythagorean theorem applications</li>
<li><strong>Handle Zero</strong>: Remember Sqr(0) = 0 is valid</li>
<li><strong>Precision Aware</strong>: Understand floating-point precision limits</li>
<li><strong>Error Handling</strong>: Trap errors for negative inputs</li>
<li><strong>Performance</strong>: Cache results when used repeatedly</li>
<li><strong>Consider Abs</strong>: For Sqr(x^2), use Abs(x) instead</li>
<li><strong>Document Units</strong>: Comment on units in physics calculations</li>
<li><strong>Test Edge Cases</strong>: Test with 0, very small, and very large values</li>
</ol>
<h2 id="comparison-with-related-functions">Comparison with Related Functions</h2>
<table>
<thead>
<tr>
<th>Operation</th>
<th>VB6 Function</th>
<th>Example</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td>Square root</td>
<td>Sqr(x)</td>
<td>Sqr(25)</td>
<td>5</td>
</tr>
<tr>
<td>Cube root</td>
<td>x^(1/3)</td>
<td>27^(1/3)</td>
<td>3</td>
</tr>
<tr>
<td>Nth root</td>
<td>x^(1/n)</td>
<td>16^(1/4)</td>
<td>2</td>
</tr>
<tr>
<td>Power</td>
<td>x^y</td>
<td>2^3</td>
<td>8</td>
</tr>
<tr>
<td>Absolute value</td>
<td>Abs(x)</td>
<td>Abs(-5)</td>
<td>5</td>
</tr>
</tbody>
</table>
<h2 id="platform-considerations">Platform Considerations</h2>
<ul>
<li>Available in VB6, VBA (all versions)</li>
<li>Part of core mathematical functions</li>
<li>Uses IEEE 754 floating-point arithmetic</li>
<li>Consistent behavior across platforms</li>
<li>Hardware-accelerated on modern processors</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Cannot calculate square root of negative numbers (use complex number libraries)</li>
<li>Subject to floating-point precision limits (~15-17 significant digits)</li>
<li>Sqr(x)^2 may not exactly equal x due to rounding</li>
<li>Very large numbers may overflow Double range</li>
<li>Very small numbers may underflow to zero</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Abs</code>: Returns absolute value of a number</li>
<li><code>Exp</code>: Returns e raised to a power</li>
<li><code>Log</code>: Returns natural logarithm</li>
<li><code>^</code> operator: Raises number to a power (x^0.5 = Sqr(x))</li>
</ul>
</article>
<div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
<p>
<a href="index.html">← Back to Math</a> |
<a href="../index.html">View all functions</a>
</p>
</div>
</main>
<footer>
<div class="container">
<p>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>