home/ atoms/ glsl-integer-division-truncates

GLSL integer division truncates: `1/2` is `0`, not `0.5`

When both operands of / are integers in GLSL, the division truncates toward zero: float f = 1/2; compiles fine but yields 0.0, because 1/2 is computed as integer 0 before the assignment converts it. To get 0.5 you must make at least one operand a float: 1.0/2.0. This is a silent bug — it compiles and runs, just with the wrong value.

Examples

float f = 1/2; // = 0.0 (integer division first) float f = 1.0/2.0; // = 0.5

Assessment

Explain why float f = 1/2; gives 0.0 in GLSL even though f is a float, and rewrite it to give 0.5.

“`float f = 1/2;` is `0`**, not `0.5` — integer division. Write `1.0/2.0`.”
context/ · L1-instruments/glsl/gotchas.md · chunk 1