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)is3.0ceil(2.0)is2.0ceil(-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
floorputs the step on the wrong side of a boundary,ceilmoves it to the other side.
Common mistakes
- Using
ceil(x) - 1.0as a replacement forfloor(x). They differ when x is already a whole number:ceil(2.0) - 1.0is1.0, butfloor(2.0)is2.0. - Reaching for
ceilwhen you actually want a threshold. For a 0/1 switch,stepis clearer.