home/ atoms/ sc-left-to-right-precedence

SuperCollider evaluates binary operators strictly left-to-right, ignoring conventional arithmetic precedence

Unlike standard mathematics and most programming languages, SuperCollider gives no precedence to multiplication or division over addition: all binary operators (+, -, *, /, mod, etc.) are evaluated strictly left to right. So 5 + 2 * 2 evaluates as (5 + 2) * 2 = 14, not 9, and 1 + 2 * 3 = 9, not 7. Parentheses must be used explicitly to override this order. Method messages, however, take precedence over binary operators: 5 + 2.squared = 9 because the .squared message is evaluated before the addition. This design is intentional and consistent with Smalltalk-style message passing, but it surprises nearly every new user coming from a math background and is the single most common source of SuperCollider bugs — including in UGen expressions, where for example harm = count + 1 * 110 means (count + 1) * 110.

Examples

5 + 2 * 2;      // 14 (not 9!)
5 + (2 * 2);    // 9 — forced order
5 + 2.squared;  // 9 — message takes precedence
1 + 2 * 3;      // 9 (not 7!)
1 + (2 * 3);    // 7 — explicit precedence
// In UGen context:
harm = count + 1 * 110;   // (count + 1) * 110

Assessment

Without running code, evaluate: (a) 3 + 4 * 2 - 1, (b) 4 + 3 * 2, (c) 4 * 3 + 2. Then write the parenthesized SC expression giving the conventional math result, and describe one real bug this rule causes in a synthesis patch. Verify in SC.

“recedence Sup erColliderfollowsalefttorightorderofprecedence”
corpus · a-gentle-introduction-to-supercollider-bruno-ruviaro · chunk 2
“SuperCollider follows a left to right order of precedence, regardless of operation. This means, for example, that multiplication doesnothappen first”
corpus · a-gentle-introduction-to-supercollider-ruviaro-archive-org-c · chunk 3
“// Left to Right Order of Precedence 1+2*3 3*(1+2)”
corpus · the-supercollider-book-official-code-examples-scbookcode-gpl · chunk 120