GLSL Learning

GLSL built-in function

log()

Returns the natural logarithm of x, the inverse of exp. It turns multiplication into addition, which makes infinite zooms possible.

Syntax

float log(float x)
vec2 log(vec2 x)
vec3 log(vec3 x)

Parameters

x
A value greater than 0.

Return value

The natural logarithm of x.

Live example

Edit the code and the preview updates as you type.

How it works

log(x) asks "e to what power gives x?": log(1.0) is 0, log(2.718) is 1.

Its useful property is that multiplying x adds a constant to log(x). Doubling a distance always adds the same amount (about 0.69), no matter how close or far you are.

The example uses log(length(p)) as one axis of a checkerboard and the angle as the other. Every ring of tiles is a fixed factor bigger than the one inside it, so moving along the log axis looks like flying forever into a tunnel.

Typical uses

  • Infinite zooms and tunnels: log-polar coordinates as in the example.
  • Compressing a large range: log(1.0 + x) for brightness or density values that span orders of magnitude.

Common mistakes

  • log(0.0) or negative input is undefined. Add a small offset: log(length(p) + 1e-4).
  • Confusing it with base 10. GLSL's log is the natural log; use log2 for base 2.

Related functions