<!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 - cos - Math">
<title>cos - 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> / cos</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="cos-function">Cos Function</h1>
<p>Returns a Double specifying the cosine of an angle.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Cos(number)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><strong>number</strong>: Required. Double or any valid numeric expression that expresses an angle in radians.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a Double representing the cosine of the angle. The return value ranges from -1 to 1.</p>
<h2 id="remarks">Remarks</h2>
<p>The <code>Cos</code> 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.
<strong>Important Characteristics:</strong>
- 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)</p>
<h2 id="angle-conversion">Angle Conversion</h2>
<p>To convert degrees to radians (required for Cos):</p>
<pre><code class="language-vbnet">radians = degrees * (π / 180)
radians = degrees * 0.0174532925199433</code></pre>
<p>To convert radians to degrees:</p>
<pre><code class="language-vbnet">degrees = radians * (180 / π)
degrees = radians * 57.2957795130823</code></pre>
<h2 id="common-angle-values">Common Angle Values</h2>
<table>
<thead>
<tr>
<th>Degrees</th>
<th>Radians</th>
<th>Cos(angle)</th>
</tr>
</thead>
<tbody>
<tr>
<td>0°</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>30°</td>
<td>π/6</td>
<td>√3/2 ≈ 0.866</td>
</tr>
<tr>
<td>45°</td>
<td>π/4</td>
<td>√2/2 ≈ 0.707</td>
</tr>
<tr>
<td>60°</td>
<td>π/3</td>
<td>0.5</td>
</tr>
<tr>
<td>90°</td>
<td>π/2</td>
<td>0</td>
</tr>
<tr>
<td>120°</td>
<td>2π/3</td>
<td>-0.5</td>
</tr>
<tr>
<td>135°</td>
<td>3π/4</td>
<td>-√2/2 ≈ -0.707</td>
</tr>
<tr>
<td>150°</td>
<td>5π/6</td>
<td>-√3/2 ≈ -0.866</td>
</tr>
<tr>
<td>180°</td>
<td>π</td>
<td>-1</td>
</tr>
<tr>
<td>270°</td>
<td>3π/2</td>
<td>0</td>
</tr>
<tr>
<td>360°</td>
<td>2π</td>
<td>1</td>
</tr>
</tbody>
</table>
<h2 id="examples">Examples</h2>
<h3 id="basic-usage">Basic Usage</h3>
<pre><code class="language-vbnet">' 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</code></pre>
<h3 id="using-pi-constant">Using Pi Constant</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="degrees-to-radians-conversion">Degrees to Radians Conversion</h3>
<pre><code class="language-vbnet">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</code></pre>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="circle-point-calculation">Circle Point Calculation</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="rotating-points">Rotating Points</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="wave-generation">Wave Generation</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="harmonic-motion">Harmonic Motion</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="distance-calculation">Distance Calculation</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="ellipse-points">Ellipse Points</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="polar-to-cartesian-conversion">Polar to Cartesian Conversion</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="clock-hand-position">Clock Hand Position</h3>
<pre><code class="language-vbnet">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</code></pre>
<h2 id="advanced-usage">Advanced Usage</h2>
<h3 id="3d-rotation-yaw">3D Rotation (Yaw)</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="fourier-series">Fourier Series</h3>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="dot-product-calculation">Dot Product Calculation</h3>
<pre><code class="language-vbnet">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</code></pre>
<h2 id="error-handling">Error Handling</h2>
<pre><code class="language-vbnet">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</code></pre>
<h3 id="common-errors">Common Errors</h3>
<ul>
<li><strong>Error 6</strong> (Overflow): Can occur with extremely large angle values</li>
<li><strong>Error 13</strong> (Type mismatch): Non-numeric argument</li>
</ul>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Cos</code> is a native function with good performance</li>
<li>For repeated calculations with the same angles, cache results</li>
<li>Consider lookup tables for fixed angle values in performance-critical code</li>
<li>Reducing angle to [0, 2π] range before calling Cos can improve accuracy</li>
</ul>
<h2 id="mathematical-properties">Mathematical Properties</h2>
<h3 id="pythagorean-identity">Pythagorean Identity</h3>
<pre><code class="language-vbnet">' 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</code></pre>
<h3 id="even-function-property">Even Function Property</h3>
<pre><code class="language-vbnet">' Cos(-x) = Cos(x)
Function VerifyEvenProperty(angle As Double) As Boolean
VerifyEvenProperty = Abs(Cos(-angle) - Cos(angle)) < 0.0000001
End Function</code></pre>
<h3 id="angle-addition-formula">Angle Addition Formula</h3>
<pre><code class="language-vbnet">' 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</code></pre>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Argument must be in radians (not degrees)</li>
<li>Very large angles may lose precision due to floating-point limitations</li>
<li>Return value is always between -1 and 1</li>
<li>Small rounding errors may occur near critical angles (e.g., π/2)</li>
<li>For angles outside normal range, consider normalizing to [0, 2π]</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Sin</code>: Returns the sine of an angle (complementary to cosine)</li>
<li><code>Tan</code>: Returns the tangent of an angle (Sin/Cos)</li>
<li><code>Atn</code>: Returns the arctangent (inverse tangent)</li>
<li><code>Acos</code>: Arc cosine (inverse of cosine, not built-in VB6)</li>
<li><code>Sqr</code>: Square root function (useful for magnitude calculations)</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>