Math and calculations
Rhyme gives you three ways to do math, and they’re suited to different situations: individual arithmetic actions for step-by-step calculations, the calculate action for compact multi-step expressions, and inline arithmetic directly in a variable assignment for the simplest cases. This page covers all three.
Individual arithmetic actions
Each of these takes existing numbers and produces a new one. Assign the result to a variable with $result = :
| Action | What it does | Example |
|---|---|---|
add | Adds a number to another | $total = add 20 to $price |
subtract | Subtracts a number from another | $remaining = subtract 120 from $balance |
multiply | Multiplies two numbers | $pay = multiply $rate by $hours |
divide | Divides one number by another | $share = divide $total by $people |
modulo | Remainder after division | $remainder = modulo $num by $divisor |
abs | Absolute (non-negative) value | $positive = abs $loss |
round | Rounds to 2 decimal places | $rounded = round $pi |
round-up | Rounds up to the nearest integer | $ceiling = round-up $value |
round-down | Rounds down to the nearest integer | $floor = round-down $value |
average | Mean of the numbers in an array | $mean = average $scores |
max | Largest value in an array | $highest = max $scores |
increment | Adds 1 to a number | $next = increment $counter |
decrement | Subtracts 1 from a number | $remaining = decrement $lives |
Each of add, subtract, multiply, divide, and modulo accepts either two variables, or a variable and a literal number:
$price = 100$total = add 20 to $price
$base = 50$bonus = 25$final = add $bonus to $baseWatch the connecting word — it’s part of the syntax, not optional filler: add ... to ..., subtract ... from ..., multiply ... by ..., divide ... by ..., modulo ... by ....
abs, round, round-up, and round-down each take a single value — a variable or a literal:
$loss = -250$positive = abs $loss
$pi = 3.14159$rounded = round $pi
$value = 3.2$ceiling = round-up $valueaverage and max take a variable holding an array of numbers:
$scores = [85, 92, 78, 95, 88]$mean = average $scores$highest = max $scoresincrement and decrement add or subtract 1. Assign the result to a new variable, or call them bare to update the variable in place:
$counter = 5$next = increment $counter
$count = 0increment $countincrement $countincrement $countDivision and modulo by zero, or operating on a missing variable, produce no value rather than an error — see Handling missing results below.
Expressions with calculate
For a calculation with more than one step, calculate (alias calc) evaluates a full expression in one line, with normal operator precedence — * and / before + and -:
$result = calculate (2 + 3 * 4)The expression goes inside parentheses, and can mix literal numbers and variables:
$x = 5$y = 3$sum = calculate ($x + $y)calc is a shorter alias for the same action:
$quick = calc (10 + 20)Use inner parentheses to control evaluation order:
$subtotal = 89.99$tax-rate = 0.08$final = calculate ($subtotal + $subtotal * $tax-rate)The -precision tag controls how many decimal places the result keeps:
$third = calculate (1/3) -precision=4// $third is 0.3333Reach for calculate when you want a compact, single-line formula. Reach for the individual actions (add, multiply, etc.) when you want each intermediate value available in its own variable — useful for debugging, or when you display a step along the way.
Inline arithmetic in assignment
For simple arithmetic, you don’t need an action at all — write the expression directly on the right-hand side of an assignment:
$x = 1 + 2This works with variables too, and follows the same operator precedence as calculate (*// before +/-):
$y = 10$z = 1 + $y
$d = 2 + 3 * 4// $d is 14Wrap part of the expression in parentheses to change the order:
$e = (2 + 3) * 4// $e is 20This is Rhyme recognizing an expression in the assignment itself — functionally it’s the same mechanism as calculate, just without the action name or parentheses requirement. For a one-off calculation, inline arithmetic is the least to type; reach for calculate/calc when the expression is long enough that the parentheses make it easier to read, or when you’re building the expression from a tag like -precision.
Bare negative numbers work as plain literals:
$f = -10Handling missing results
Division by zero, modulo by zero, and math on a missing/undefined variable all produce no value instead of throwing an error. Check for this with if missing:
$a = 10$b = 0$result = divide $a by $b
if missing $result: .text "Cannot divide by zero" #red:else .text "Result: {$result}":endCommon mistakes
| Mistake | Fix |
|---|---|
Dropping the connecting word: $r = add $a $b | Use to/from/by: $r = add $a to $b |
Wrong connecting word: $r = subtract $a to $b | subtract pairs with from: $r = subtract $a from $b |
Forgetting parentheses in calculate: $r = calculate 2 + 3 | Wrap the expression: $r = calculate (2 + 3) |
Calling average/max on a non-array variable | Pass an array: $nums = [1, 2, 3] then $avg = average $nums |
| Dividing by zero without checking the result | Check first: if missing $result: after a divide |
| Assuming a divide/modulo/missing-variable error stops the app | It doesn’t — the target variable is simply left without a value |
Complete examples
Tip calculator
$bill = 85$tip-percent = 18$hundred = 100$tip-rate = divide $tip-percent by $hundred$tip-amount = multiply $bill by $tip-rate$tip-rounded = round $tip-amount$total = add $tip-rounded to $bill
.text "Bill: {$bill}" -bold.text "Tip (18%): {$tip-rounded}".text "Total: {$total}" -boldGrade report
$grades = [88, 92, 76, 95, 81, 89, 77]
$class-average = average $grades$avg-display = round $class-average$top-score = max $grades
.text "Grade Report" -bold.text "Class average: {$avg-display}".text "Top score: {$top-score}"
if $avg-display > 85: .text "Great job, class!" #green:else .text "Keep studying!" #orange:endShopping cart with a discount
$item-a = 25$item-b = 40$item-c = 15
$subtotal = 0$subtotal = add $item-a to $subtotal$subtotal = add $item-b to $subtotal$subtotal = add $item-c to $subtotal
$discount-rate = 0.10$discount = multiply $subtotal by $discount-rate$discount = round $discount$final = subtract $discount from $subtotal
.text "Subtotal: {$subtotal}".text "Discount (10%): {$discount}".text "Final total: {$final}" -bold