home/ atoms/ chuck-integer-division-silent

Integer division in ChucK silently truncates: 1/2 evaluates to 0, not 0.5

ChucK is strongly typed and performs integer division when both operands are integers. 1/2 evaluates to 0, which can silently produce a 0 Hz frequency (silence) or a wrong tempo. The fix is to use float literals: 1.0/2.0. Similarly, dur/time arithmetic must stay in dur/time units (::ms, ::second) and never mix with bare integer arithmetic. Use Std.mtof(60) to convert a MIDI note number to a frequency rather than assigning the raw integer to .freq.

Examples

1/2 => s.freq → 0 Hz silence. Correct: 1.0/2.0, or Std.mtof(60) => s.freq for a MIDI note.

Assessment

Predict the value of 1/2 in ChucK and explain the silent failure it causes when assigned to an oscillator’s frequency, then write the corrected expression.

“Integer division / type surprise** | `1/2` → 0, `60000::ms/bpm` type mix | No error; wrong pitch/tempo”
context/ · L5-debug/chuck.md · chunk 1