Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Arithmetic Operators

The Six Operations

LyknJSOperation
(+ a b)a + bAddition
(- a b)a - bSubtraction
(* a b)a * bMultiplication
(/ a b)a / bDivision
(% a b)a % bRemainder
(** a b)a ** bExponentiation

All six compile to their JavaScript counterparts. The semantics are JavaScript’s — IEEE 754 floating-point arithmetic, with all the precision implications that entails.

A Note on %

JavaScript’s % is a remainder operator, not a modulo operator. The difference matters for negative numbers: (-7 % 3) produces -1 in JavaScript (the sign follows the dividend), while a true modulo operation would produce 2. If you need mathematical modulo, use ((+ (% a b) b) % b) or write a utility function.

Unary Negation

A single-argument - produces unary negation:

(- x)                 ;; → -x
(- (- x))             ;; → -(-x)   (double negation)

Real Examples

(bind total (+ price (* quantity tax-rate)))
(bind average (/ (+ a b c) 3))
(bind is-even (= (% n 2) 0))
(bind area (* Math:PI (** radius 2)))
const total = price + quantity * taxRate;
const average = (a + b + c) / 3;
const isEven = n % 2 === 0;
const area = Math.PI * radius ** 2;

Remember from Chapter 5: + is overloaded for string concatenation in JavaScript. (+ "5" 3) produces "53", not 8. Type keywords on function boundaries catch this at the gate; inside a function body, the coercion rules apply as they do in JavaScript.