GLSL Learning

GLSL built-in function

ceil()

Rounds x up to the nearest whole number that is greater than or equal to x.

Syntax

float ceil(float x)
vec2 ceil(vec2 x)
vec3 ceil(vec3 x)
vec4 ceil(vec4 x)

Parameters

x
The value to round up. Vectors are processed per component.

Return value

A value of the same type as x containing whole numbers (still stored as floats).

Live example

Edit the code and the preview updates as you type.

How it works

ceil(x) is the mirror of floor: it always rounds up.

  • ceil(2.1) is 3.0
  • ceil(2.0) is 2.0
  • ceil(-0.7) is -0.0 (up, toward positive infinity)

In the example the gray staircase is floor and the green one is ceil. Between whole numbers the two are always exactly one step apart; on whole numbers they meet.

Typical uses

  • Counting how many pieces are needed: ceil(length / segment) gives the number of segments that fully cover a length.
  • Shifting a staircase: when floor puts the step on the wrong side of a boundary, ceil moves it to the other side.

Common mistakes

  • Using ceil(x) - 1.0 as a replacement for floor(x). They differ when x is already a whole number: ceil(2.0) - 1.0 is 1.0, but floor(2.0) is 2.0.
  • Reaching for ceil when you actually want a threshold. For a 0/1 switch, step is clearer.

Related functions