GLSL built-in function
exp2()
Returns 2 raised to the power x. Doubling and halving per step, as in noise octaves and exposure stops.
Syntax
float exp2(float x)
vec2 exp2(vec2 x)
vec3 exp2(vec3 x)Parameters
- x
- The exponent.
Return value
2 to the power x, always greater than 0.
Live example
Edit the code and the preview updates as you type.
How it works
exp2(x) doubles for every step of x: exp2(0.0) is 1, exp2(1.0) is 2, exp2(3.0) is 8, and exp2(-1.0) is 0.5. It is the same as pow(2.0, x) but says the intent more directly.
The example uses the row number as x, so each row has twice as many stripes as the one below: 1, 2, 4, 8, 16.
Typical uses
- Noise octaves: frequency
exp2(float(i))and amplitudeexp2(-float(i))for fractal noise. - Exposure:
color * exp2(stops)brightens by whole camera stops. - Smooth zoom:
exp2(u_time)zooms in at a steady visual speed.
Common mistakes
- Large exponents overflow mediump precision quickly;
exp2(16.0)is already 65536. - Passing an
intloop counter directly. Convert it first:exp2(float(i)).
Related functions
log2()
Returns the base-2 logarithm of x: how many times you double 1.0 to reach x. The inverse of exp2.
pow()
Returns x raised to the power y. On values between 0.0 and 1.0 it bends a straight ramp into a curve.
exp()
Returns e (about 2.718) raised to the power x. exp(-k * d) is the standard smooth falloff for glows and fog.