Intrinsics1 min read
log
Computes the natural logarithm.
Reading Time
1 min
Word Count
134
Sections
12
Try It Live
Test log in a live shader
Open the playground, start from a visual preset, and wire log into the fragment stage to see how it behaves with real values.
Open PlaygroundLive Demo
The log function returns the natural (base-e) logarithm of the input value.
Signature
bwsl
log :: (T x) -> T {...}
Where T can be float, float2, float3, or float4.
Parameters
| Parameter | Type | Description |
|---|---|---|
x | T | The input value (must be > 0) |
Return Value
Returns the natural logarithm (ln) of x.
Example
bwsl
pipeline LogarithmicDepth {
fragment {
// Logarithmic depth buffer for better precision
float linearDepth = length(input.worldPos - cameraPos);
float C = 1.0; // Constant to tune precision
float logDepth = log(C * linearDepth + 1.0) / log(C * farPlane + 1.0);
output.color = float4(float3(logDepth), 1.0);
}
}Common Use Cases
Logarithmic Depth
bwsl
// Logarithmic depth for large scenes
float logZ = log(linearZ / nearPlane) / log(farPlane / nearPlane);Perceptual Color Space
bwsl
// Convert to log luminance space
float logLuminance = log(luminance + epsilon);Decibel Conversion
bwsl
// Convert linear to decibels
float dB = 20.0 * log(amplitude) / log(10.0);Growth Rate Calculation
bwsl
// Calculate exponential growth rate
float rate = log(finalValue / initialValue) / time;Information Theory
bwsl
// Entropy calculation
float entropy = -probability * log(probability);Domain
log(x) is undefined for x <= 0. For x = 0, it returns negative infinity. Always ensure positive input, often by adding a small epsilon: log(x + 0.0001).
Base Conversion
To compute logarithm in other bases: log_b(x) = log(x) / log(b). For base 10: log10(x) = log(x) / 2.302585.
Compiled Output
When compiled to GLSL:
glsl
log(x)
When compiled to HLSL:
hlsl
log(x)