home/ atoms/ chuck-integer-division

In ChucK, dividing two integer literals gives integer division, truncating toward zero

ChucK, like C, performs integer division when both operands are integers: 1/2 evaluates to 0, not 0.5. This silently corrupts tempo math and MIDI arithmetic where a fractional result was intended. The fix is to make at least one operand a float: 1.0/2.0 or 1/2.0. The trap is ‘silent’ because it compiles and runs — it just yields the wrong number.

Examples

1/2 → 0 // integer division 1.0/2 → 0.5 // float division 60000::ms / bpm with int bpm // truncates

Assessment

Predict the output of <<<1/2>>> in ChucK and explain why. Then rewrite 60000::ms / bpm (with bpm an int) to compute a float number of seconds per beat.

“`int / int` is integer division.** `1/2` is `0`. Write `1.0/2.0` or `1/2.0` for floats. Common in tempo math (`60000::ms / bpm` etc. — mind the types).”
context/ · L1-instruments/chuck/gotchas.md · chunk 1