Intrinsics1 min read

exp

Computes e raised to a power.

Reading Time
1 min
Word Count
125
Sections
12
Try It Live

Test exp in a live shader

Open the playground, start from a visual preset, and wire exp into the fragment stage to see how it behaves with real values.

Open Playground

Live Demo

The exp function returns the natural exponential function e^x, where e is Euler's number (approximately 2.71828).

Signature

bwsl
exp :: (T x) -> T {...}

Where T can be float, float2, float3, or float4.

Parameters

ParameterTypeDescription
xTThe exponent

Return Value

Returns e raised to the power x.

Example

bwsl
pipeline ExponentialFog {
fragment {
// Calculate view distance
float dist = length(input.worldPos - cameraPos);
// Exponential fog falloff
float fogDensity = 0.05;
float fogFactor = 1.0 - exp(-dist * fogDensity);
float3 sceneColor = sample(sceneTex, input.uv).rgb;
float3 fogColor = float3(0.7, 0.8, 0.9);
float3 finalColor = lerp(sceneColor, fogColor, fogFactor);
output.color = float4(finalColor, 1.0);
}
}

Common Use Cases

Exponential Fog

bwsl
// Standard exponential fog
float fog = exp(-distance * density);
// Exponential squared fog (denser)
float fog = exp(-pow(distance * density, 2.0));

Gaussian Blur Weight

bwsl
// Gaussian distribution
float weight = exp(-0.5 * (offset * offset) / (sigma * sigma));

Decay Functions

bwsl
// Exponential decay over time
float value = initialValue * exp(-decayRate * time);

Soft Shadows

bwsl
// Exponential soft shadow falloff
float shadow = exp(-shadowDistance * softness);

Atmospheric Scattering

bwsl
// Beer-Lambert law for light extinction
float3 extinction = exp(-opticalDepth * scatteringCoeffs);

Relationship to log

exp and log are inverse functions: exp(log(x)) = x and log(exp(x)) = x.

Numerical Stability

For very large positive inputs, exp(x) can overflow to infinity. For very negative inputs, it approaches zero. Consider clamping inputs for numerical stability.

Compiled Output

When compiled to GLSL:

glsl
exp(x)

When compiled to HLSL:

hlsl
exp(x)

See Also

  • exp2 - 2^x
  • log - Natural logarithm
  • pow - General power function