<!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 - log - Math">
<title>log - 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> / log</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="log-function">Log Function</h1>
<p>Returns a Double specifying the natural logarithm of a number.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Log(number)</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>number</code> (Required): Double or any valid numeric expression greater than zero</li>
<li>Must be positive (> 0)</li>
<li>Cannot be zero or negative</li>
<li>Error 5 "Invalid procedure call or argument" if number <= 0</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a Double:
- Natural logarithm (base e) of the number
- Also known as ln(x) in mathematics
- Result can be positive, negative, or zero
- Log(1) = 0
- Log(e) = 1 where e ≈ 2.71828182845905
- For values 0 < x < 1, result is negative
- For values x > 1, result is positive</p>
<h2 id="remarks">Remarks</h2>
<p>The Log function returns the natural logarithm:
- Natural logarithm uses base e (Euler's number)
- e ≈ 2.71828182845905
- Also written as ln(x) in mathematical notation
- Inverse operation of Exp function
- Log(Exp(x)) = x
- Exp(Log(x)) = x (for x > 0)
- To calculate logarithms with other bases, use change of base formula
- Log base 10: Log(x) / Log(10)
- Log base 2: Log(x) / Log(2)
- Log base n: Log(x) / Log(n)
- Error 5 if argument is zero or negative
- Used in scientific and engineering calculations
- Common in exponential growth/decay problems
- Essential for statistical calculations
- Used in information theory (entropy, information content)
- Financial calculations (continuous compounding)
- Physics (radioactive decay, sound levels)
- Can be used to solve exponential equations
- Part of VB6's math function library
- Available in all VB versions</p>
<h2 id="typical-uses">Typical Uses</h2>
<ol>
<li><strong>Natural Logarithm</strong></li>
</ol>
<pre><code class="language-vbnet"> result = Log(10)
```
2. **Base 10 Logarithm**
```vb
log10 = Log(x) / Log(10)
```
3. **Base 2 Logarithm**
```vb
log2 = Log(x) / Log(2)
```
4. **Exponential Decay**
```vb
timeConstant = -1 / Log(decayRate)
```
5. **Solve for Exponent**
```vb
exponent = Log(result / initial) / Log(base)
```
6. **Information Content**
```vb
bits = -Log(probability) / Log(2)
```
7. **pH Calculation**
```vb
pH = -Log(hydrogenIonConcentration) / Log(10)
```
8. **Continuous Compounding**
```vb
rate = Log(finalValue / initialValue) / time
```
## Basic Examples
### Example 1: Natural Logarithm</code></pre>
<p>vb
Dim result As Double
result = Log(1) ' Returns 0
result = Log(2.71828) ' Returns ~1 (ln(e) = 1)
result = Log(10) ' Returns ~2.302585
result = Log(100) ' Returns ~4.605170</p>
<pre><code>### Example 2: Base 10 Logarithm</code></pre>
<p>vb
Function Log10(ByVal x As Double) As Double
Log10 = Log(x) / Log(10)
End Function
' Usage
Dim result As Double
result = Log10(100) ' Returns 2
result = Log10(1000) ' Returns 3
result = Log10(10) ' Returns 1</p>
<pre><code>### Example 3: Exponential Growth</code></pre>
<p>vb
' Calculate doubling time
Function DoublingTime(ByVal growthRate As Double) As Double
' growthRate is the rate per time period (e.g., 0.05 = 5%)
DoublingTime = Log(2) / Log(1 + growthRate)
End Function
' Usage
Dim years As Double
years = DoublingTime(0.07) ' 7% annual growth
MsgBox "Doubling time: " & Format(years, "0.0") & " years"</p>
<pre><code>### Example 4: Solve Exponential Equation</code></pre>
<p>vb
' Solve: base^exponent = result for exponent
Function SolveExponent(ByVal base As Double, _
ByVal result As Double) As Double
If base > 0 And base <> 1 And result > 0 Then
SolveExponent = Log(result) / Log(base)
Else
SolveExponent = 0
End If
End Function
' Usage: Solve 2^x = 32
Dim x As Double
x = SolveExponent(2, 32) ' Returns 5
MsgBox "2^" & x & " = 32"</p>
<pre><code>## Common Patterns
### Pattern 1: Log10 (Base 10 Logarithm)</code></pre>
<p>vb
Function Log10(ByVal x As Double) As Double
If x > 0 Then
Log10 = Log(x) / Log(10)
Else
Err.Raise 5, , "Invalid argument"
End If
End Function</p>
<pre><code>### Pattern 2: Log2 (Base 2 Logarithm)</code></pre>
<p>vb
Function Log2(ByVal x As Double) As Double
If x > 0 Then
Log2 = Log(x) / Log(2)
Else
Err.Raise 5, , "Invalid argument"
End If
End Function</p>
<pre><code>### Pattern 3: `LogN` (Logarithm with Any Base)</code></pre>
<p>vb
Function LogN(ByVal x As Double, ByVal base As Double) As Double
If x > 0 And base > 0 And base <> 1 Then
LogN = Log(x) / Log(base)
Else
Err.Raise 5, , "Invalid arguments"
End If
End Function</p>
<pre><code>### Pattern 4: `SafeLog`</code></pre>
<p>vb
Function SafeLog(ByVal x As Double, _
Optional ByVal defaultValue As Double = 0) As Double
On Error Resume Next
SafeLog = Log(x)
If Err.Number <> 0 Then
SafeLog = defaultValue
Err.Clear
End If
End Function</p>
<pre><code>### Pattern 5: `CalculateEntropy`</code></pre>
<p>vb
Function CalculateEntropy(probabilities() As Double) As Double
Dim i As Integer
Dim entropy As Double
Dim p As Double
entropy = 0
For i = LBound(probabilities) To UBound(probabilities)
p = probabilities(i)
If p > 0 Then
entropy = entropy - p * (Log(p) / Log(2))
End If
Next i
CalculateEntropy = entropy
End Function</p>
<pre><code>### Pattern 6: `CalculateHalfLife`</code></pre>
<p>vb
Function CalculateHalfLife(ByVal decayConstant As Double) As Double
If decayConstant > 0 Then
CalculateHalfLife = Log(2) / decayConstant
Else
CalculateHalfLife = 0
End If
End Function</p>
<pre><code>### Pattern 7: `CalculateDoublingTime`</code></pre>
<p>vb
Function CalculateDoublingTime(ByVal growthRate As Double) As Double
If growthRate > 0 Then
CalculateDoublingTime = Log(2) / Log(1 + growthRate)
Else
CalculateDoublingTime = 0
End If
End Function</p>
<pre><code>### Pattern 8: `SolveForTime` (Exponential Growth)</code></pre>
<p>vb
Function SolveForTime(ByVal initialValue As Double, _
ByVal finalValue As Double, _
ByVal rate As Double) As Double
If initialValue > 0 And finalValue > 0 And rate <> 0 Then
SolveForTime = Log(finalValue / initialValue) / rate
Else
SolveForTime = 0
End If
End Function</p>
<pre><code>### Pattern 9: `CalculateDecibels`</code></pre>
<p>vb
Function CalculateDecibels(ByVal power As Double, _
ByVal referencePower As Double) As Double
If power > 0 And referencePower > 0 Then
CalculateDecibels = 10 * (Log(power / referencePower) / Log(10))
Else
CalculateDecibels = 0
End If
End Function</p>
<pre><code>### Pattern 10: `CalculatePH`</code></pre>
<p>vb
Function CalculatePH(ByVal hydrogenIonConcentration As Double) As Double
If hydrogenIonConcentration > 0 Then
CalculatePH = -(Log(hydrogenIonConcentration) / Log(10))
Else
CalculatePH = 7 ' Neutral pH
End If
End Function</p>
<pre><code>## Advanced Examples
### Example 1: Logarithm Calculator</code></pre>
<p>vb
' Module: LogarithmCalculator
Public Function NaturalLog(ByVal x As Double) As Double
If x <= 0 Then
Err.Raise 5, "LogarithmCalculator", _
"Argument must be positive"
End If
NaturalLog = Log(x)
End Function
Public Function Log10(ByVal x As Double) As Double
If x <= 0 Then
Err.Raise 5, "LogarithmCalculator", _
"Argument must be positive"
End If
Log10 = Log(x) / Log(10)
End Function
Public Function Log2(ByVal x As Double) As Double
If x <= 0 Then
Err.Raise 5, "LogarithmCalculator", _
"Argument must be positive"
End If
Log2 = Log(x) / Log(2)
End Function
Public Function LogBase(ByVal x As Double, _
ByVal base As Double) As Double
If x <= 0 Or base <= 0 Or base = 1 Then
Err.Raise 5, "LogarithmCalculator", _
"Invalid arguments"
End If
LogBase = Log(x) / Log(base)
End Function
Public Function Antilog(ByVal x As Double) As Double
' Returns e^x (inverse of Log)
Antilog = Exp(x)
End Function
Public Function Antilog10(ByVal x As Double) As Double
' Returns 10^x (inverse of Log10)
Antilog10 = 10 ^ x
End Function</p>
<pre><code>### Example 2: Exponential Growth Analyzer</code></pre>
<p>vb
' Class: GrowthAnalyzer
Private m_initialValue As Double
Private m_currentValue As Double
Private m_timeElapsed As Double
Public Sub Initialize(ByVal initialValue As Double)
m_initialValue = initialValue
m_currentValue = initialValue
m_timeElapsed = 0
End Sub
Public Property Let CurrentValue(ByVal value As Double)
m_currentValue = value
End Property
Public Property Let TimeElapsed(ByVal time As Double)
m_timeElapsed = time
End Property
Public Property Get GrowthRate() As Double
If m_timeElapsed > 0 And m_initialValue > 0 And m_currentValue > 0 Then
GrowthRate = Log(m_currentValue / m_initialValue) / m_timeElapsed
Else
GrowthRate = 0
End If
End Property
Public Property Get DoublingTime() As Double
Dim rate As Double
rate = GrowthRate
If rate > 0 Then
DoublingTime = Log(2) / rate
Else
DoublingTime = 0
End If
End Property
Public Function ProjectValue(ByVal futureTime As Double) As Double
Dim rate As Double
rate = GrowthRate
If rate <> 0 Then
ProjectValue = m_initialValue * Exp(rate * futureTime)
Else
ProjectValue = m_initialValue
End If
End Function
Public Function TimeToReach(ByVal targetValue As Double) As Double
Dim rate As Double
rate = GrowthRate
If rate > 0 And m_initialValue > 0 And targetValue > 0 Then
TimeToReach = Log(targetValue / m_initialValue) / rate
Else
TimeToReach = 0
End If
End Function</p>
<pre><code>### Example 3: Sound Level Calculator</code></pre>
<p>vb
' Module: SoundLevelCalculator
Private Const REFERENCE_PRESSURE As Double = 0.00002 ' 20 micropascals
Public Function CalculateDecibels(ByVal pressure As Double) As Double
If pressure > 0 Then
CalculateDecibels = 20 * (Log(pressure / REFERENCE_PRESSURE) / Log(10))
Else
CalculateDecibels = 0
End If
End Function
Public Function CombineSoundLevels(levels() As Double) As Double
Dim i As Integer
Dim sumPressures As Double
Dim pressure As Double
sumPressures = 0
For i = LBound(levels) To UBound(levels)
' Convert dB to pressure, sum, then convert back
pressure = REFERENCE_PRESSURE * Exp(levels(i) * Log(10) / 20)
sumPressures = sumPressures + pressure * pressure
Next i
If sumPressures > 0 Then
CombineSoundLevels = 10 * (Log(sumPressures) / Log(10)) + _
20 * (Log(REFERENCE_PRESSURE) / Log(10))
Else
CombineSoundLevels = 0
End If
End Function
Public Function CalculateDistance(ByVal soundLevelAtSource As Double, _
ByVal soundLevelAtDistance As Double, _
ByVal knownDistance As Double) As Double
Dim ratio As Double
' Sound decreases by 6 dB when distance doubles
ratio = (soundLevelAtSource - soundLevelAtDistance) / 6
CalculateDistance = knownDistance * (2 ^ ratio)
End Function</p>
<pre><code>### Example 4: Financial Calculator</code></pre>
<p>vb
' Module: FinancialCalculator
Public Function CalculateContinuousGrowthRate(ByVal initialValue As Double, _
ByVal finalValue As Double, _
ByVal years As Double) As Double
If initialValue > 0 And finalValue > 0 And years > 0 Then
CalculateContinuousGrowthRate = Log(finalValue / initialValue) / years
Else
CalculateContinuousGrowthRate = 0
End If
End Function
Public Function YearsToDouble(ByVal annualRate As Double) As Double
If annualRate > 0 Then
YearsToDouble = Log(2) / Log(1 + annualRate)
Else
YearsToDouble = 0
End If
End Function
Public Function EffectiveRate(ByVal nominalRate As Double, _
ByVal compoundingPeriods As Integer) As Double
If compoundingPeriods > 0 Then
EffectiveRate = Exp(compoundingPeriods * _
Log(1 + nominalRate / compoundingPeriods)) - 1
Else
EffectiveRate = 0
End If
End Function
Public Function CalculateAPY(ByVal principal As Double, _
ByVal finalAmount As Double, _
ByVal years As Double) As Double
If principal > 0 And finalAmount > 0 And years > 0 Then
CalculateAPY = Exp(Log(finalAmount / principal) / years) - 1
Else
CalculateAPY = 0
End If
End Function</p>
<pre><code>## Error Handling</code></pre>
<p>vb
' Error 5: Invalid procedure call or argument
On Error Resume Next
result = Log(0)
If Err.Number = 5 Then
MsgBox "Cannot take log of zero!"
End If
result = Log(-10)
If Err.Number = 5 Then
MsgBox "Cannot take log of negative number!"
End If
' Safe log function
Function SafeLog(ByVal x As Double) As Variant
On Error Resume Next
SafeLog = Log(x)
If Err.Number <> 0 Then
SafeLog = Null
Err.Clear
End If
End Function</p>
<pre><code>## Performance Considerations
- **Fast Operation**: Log is a built-in processor instruction
- **Cache Constants**: Store Log(10), Log(2) if used repeatedly
- **Avoid Division by Zero**: Always validate arguments
- **Use Exp for Inverse**: Exp(Log(x)) = x is optimized
## Best Practices
1. **Always validate** that argument is positive
2. **Use error handling** for user input
3. **Cache frequently used** logarithms (Log(10), Log(2))
4. **Document the base** when using change of base formula
5. **Use descriptive names** for logarithm wrapper functions
6. **Consider precision** for very large or small numbers
7. **Check for overflow** in calculations
8. **Use constants** for common values (e, pi, etc.)
9. **Validate results** for domain-specific constraints
10. **Comment formulas** explaining mathematical relationships
## Comparison with Related Functions
| Function | Purpose | Base | Domain |
|----------|---------|------|--------|
| **Log** | Natural logarithm | e (2.718...) | x > 0 |
| **Exp** | Exponential (e^x) | e | All reals |
| **Log10** | Common logarithm | 10 | x > 0 |
| **Log2** | Binary logarithm | 2 | x > 0 |
| **^** (power) | Exponentiation | Variable | Depends |
## Common Logarithm Identities</code></pre>
<p>vb
' Log properties
Log(x * y) = Log(x) + Log(y)
Log(x / y) = Log(x) - Log(y)
Log(x ^ y) = y * Log(x)
Log(1) = 0
Log(e) = 1
' Change of base
Log_b(x) = Log(x) / Log(b)
' Inverse relationship
Exp(Log(x)) = x (for x > 0)
Log(Exp(x)) = x
```</p>
<h2 id="platform-notes">Platform Notes</h2>
<ul>
<li>Available in all VB6 versions</li>
<li>Part of VBA core library</li>
<li>Uses IEEE 754 double-precision floating point</li>
<li>Precision: approximately 15-16 significant digits</li>
<li>Range: 4.94065645841247E-324 to 1.79769313486232E+308</li>
<li>Behavior identical across Windows versions</li>
<li>CPU-level implementation (very fast)</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li><strong>Positive Arguments Only</strong>: Cannot compute log of zero or negative numbers</li>
<li><strong>Floating Point Precision</strong>: Subject to rounding errors</li>
<li><strong>Very Small Numbers</strong>: May lose precision near zero</li>
<li><strong>Very Large Numbers</strong>: May overflow in calculations</li>
<li><strong>No Base Parameter</strong>: Must use change of base formula for other bases</li>
<li><strong>Error for Invalid Input</strong>: Raises Error 5 instead of returning special value</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Exp</code>: Returns e raised to a power (inverse of Log)</li>
<li><code>Sqr</code>: Returns square root</li>
<li><code>^</code>: Exponentiation operator</li>
<li><code>Abs</code>: Returns absolute value</li>
<li><code>Sgn</code>: Returns sign of number</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>