Sprites.Maths
Arithmetic over reactive values. Each operator reads its arguments and returns a reactive value that follows them, so a sum or a product built from several values stays live and composes with the reactive structure. Every argument is a constant or a reactive value, so the operators nest.
- Sprites.Maths.Add
- Sprites.Maths.Subtract
- Sprites.Maths.Multiply
- Sprites.Maths.Divide
- Sprites.Maths.Modulo
- Sprites.Maths.Floor
- Sprites.Maths.Ceiling
- Sprites.Maths.Round
- Sprites.Maths.Power
- Sprites.Maths.Min
- Sprites.Maths.Max
- Sprites.Maths.Clamp
- Sprites.Maths.GreaterOrEqual
- Sprites.Maths.Greater
Sprites.Maths.Add Sprites.Reactive.Value
Sprites.Maths.Add(a, b)
The sum of two values. Either may be a constant or a reactive value, so the result follows both.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The first value. |
b | Number or Sprites.Reactive.Value | The second value. |
Returns a read-only Sprites.Reactive.Value holding the sum.
const a = Sprites.Reactive.Value(3);
const b = Sprites.Reactive.Value(4);
const sum = Sprites.Maths.Add(a, b);
Sprites.Ui.Input.Number(a);
Sprites.Ui.Input.Number(b);
Sprites.Ui.Dom.Text(' sum = ', sum);
Sprites.Maths.Subtract Sprites.Reactive.Value
Sprites.Maths.Subtract(a, b)
The difference of two values, a less b. Either may be a constant or a reactive value, so the result follows both.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The value to subtract from. |
b | Number or Sprites.Reactive.Value | The value to subtract. |
Returns a read-only Sprites.Reactive.Value holding the difference.
const a = Sprites.Reactive.Value(9);
const b = Sprites.Reactive.Value(4);
const difference = Sprites.Maths.Subtract(a, b);
Sprites.Ui.Input.Number(a);
Sprites.Ui.Input.Number(b);
Sprites.Ui.Dom.Text(' difference = ', difference);
Sprites.Maths.Multiply Sprites.Reactive.Value
Sprites.Maths.Multiply(a, b)
The product of two values. Either may be a constant or a reactive value, so the result follows both.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The first value. |
b | Number or Sprites.Reactive.Value | The second value. |
Returns a read-only Sprites.Reactive.Value holding the product.
const width = Sprites.Reactive.Value(8);
const aspect = Sprites.Reactive.Value(2);
const span = Sprites.Maths.Multiply(width, aspect);
Sprites.Ui.Input.Number(width);
Sprites.Ui.Input.Number(aspect);
Sprites.Ui.Dom.Text(' span = ', span);
Sprites.Maths.Divide Sprites.Reactive.Value
Sprites.Maths.Divide(a, b)
The quotient of two values, a over b. Either may be a constant or a reactive value, so the result follows both.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The value to divide. |
b | Number or Sprites.Reactive.Value | The value to divide by. |
Returns a read-only Sprites.Reactive.Value holding the quotient.
const a = Sprites.Reactive.Value(6);
const b = Sprites.Reactive.Value(4);
const quotient = Sprites.Maths.Divide(a, b);
Sprites.Ui.Input.Number(a);
Sprites.Ui.Input.Number(b);
Sprites.Ui.Dom.Text(' quotient = ', quotient);
Sprites.Maths.Modulo Sprites.Reactive.Value
Sprites.Maths.Modulo(a, b)
The remainder of a divided by b. Either may be a constant or a reactive value, so the result follows both.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The value to divide. |
b | Number or Sprites.Reactive.Value | The value to divide by. |
Returns a read-only Sprites.Reactive.Value holding the remainder.
const index = Sprites.Reactive.Value(10);
const width = Sprites.Reactive.Value(4);
const column = Sprites.Maths.Modulo(index, width);
Sprites.Ui.Input.Number(index);
Sprites.Ui.Input.Number(width);
Sprites.Ui.Dom.Text(' column = ', column);
Sprites.Maths.Floor Sprites.Reactive.Value
Sprites.Maths.Floor(a)
A value rounded down to the nearest whole number. The argument may be a constant or a reactive value, so the result follows it.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The value to round down. |
Returns a read-only Sprites.Reactive.Value holding the whole number.
const index = Sprites.Reactive.Value(7);
const width = Sprites.Reactive.Value(4);
const quotient = Sprites.Maths.Divide(index, width);
const row = Sprites.Maths.Floor(quotient);
Sprites.Ui.Input.Number(index);
Sprites.Ui.Input.Number(width);
Sprites.Ui.Dom.Text(' row = ', row);
Sprites.Maths.Ceiling Sprites.Reactive.Value
Sprites.Maths.Ceiling(a)
A value rounded up to the nearest whole number. The argument may be a constant or a reactive value, so the result follows it.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The value to round up. |
Returns a read-only Sprites.Reactive.Value holding the whole number.
const total = Sprites.Reactive.Value(10);
const perPage = Sprites.Reactive.Value(4);
const quotient = Sprites.Maths.Divide(total, perPage);
const pages = Sprites.Maths.Ceiling(quotient);
Sprites.Ui.Input.Number(total);
Sprites.Ui.Input.Number(perPage);
Sprites.Ui.Dom.Text(' pages = ', pages);
Sprites.Maths.Round Sprites.Reactive.Value
Sprites.Maths.Round(a)
A value rounded to the nearest whole number. The argument may be a constant or a reactive value, so the result follows it.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The value to round. |
Returns a read-only Sprites.Reactive.Value holding the whole number.
const a = Sprites.Reactive.Value(7);
const b = Sprites.Reactive.Value(2);
const quotient = Sprites.Maths.Divide(a, b);
const nearest = Sprites.Maths.Round(quotient);
Sprites.Ui.Input.Number(a);
Sprites.Ui.Input.Number(b);
Sprites.Ui.Dom.Text(' nearest = ', nearest);
Sprites.Maths.Power Sprites.Reactive.Value
Sprites.Maths.Power(a, b)
A raised to the power b. Either may be a constant or a reactive value, so the result follows both.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The base. |
b | Number or Sprites.Reactive.Value | The exponent. |
Returns a read-only Sprites.Reactive.Value holding the power.
const base = Sprites.Reactive.Value(2);
const bits = Sprites.Reactive.Value(3);
const colours = Sprites.Maths.Power(base, bits);
Sprites.Ui.Input.Number(base);
Sprites.Ui.Input.Number(bits);
Sprites.Ui.Dom.Text(' colours = ', colours);
Sprites.Maths.Min Sprites.Reactive.Value
Sprites.Maths.Min(a, b)
The smaller of two values. Either may be a constant or a reactive value, so the result follows both.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The first value. |
b | Number or Sprites.Reactive.Value | The second value. |
Returns a read-only Sprites.Reactive.Value holding the smaller value.
const wanted = Sprites.Reactive.Value(16);
const slots = Sprites.Reactive.Value(8);
const colours = Sprites.Maths.Min(wanted, slots);
Sprites.Ui.Input.Number(wanted);
Sprites.Ui.Input.Number(slots);
Sprites.Ui.Dom.Text(' colours = ', colours);
Sprites.Maths.Max Sprites.Reactive.Value
Sprites.Maths.Max(a, b)
The larger of two values. Either may be a constant or a reactive value, so the result follows both.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The first value. |
b | Number or Sprites.Reactive.Value | The second value. |
Returns a read-only Sprites.Reactive.Value holding the larger value.
const pages = Sprites.Reactive.Value(0);
const atLeastOne = Sprites.Maths.Max(pages, 1);
Sprites.Ui.Input.Number(pages);
Sprites.Ui.Dom.Text(' pages = ', atLeastOne);
Sprites.Maths.Clamp Sprites.Reactive.Value
Sprites.Maths.Clamp(value, min, max)
A value held within the range min to max. A value below min, or not a number, settles at min; a value above max settles at max. Each argument may be a constant or a reactive value, so the result follows them all.
| Input | Type | Description |
|---|---|---|
value | Number or Sprites.Reactive.Value | The value to hold in range. |
min | Number or Sprites.Reactive.Value | The lowest allowed value. |
max | Number or Sprites.Reactive.Value | The highest allowed value. |
Returns a read-only Sprites.Reactive.Value holding the value clamped to the range.
const page = Sprites.Reactive.Value(8);
const pages = Sprites.Reactive.Value(5);
const shown = Sprites.Maths.Clamp(page, 1, pages);
Sprites.Ui.Input.Number(page);
Sprites.Ui.Input.Number(pages);
Sprites.Ui.Dom.Text(' shown = ', shown);
Sprites.Maths.GreaterOrEqual Sprites.Reactive.Value
Sprites.Maths.GreaterOrEqual(a, b)
True when a is greater than or equal to b. Either may be a constant or a reactive value, so the result follows both.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The value to compare. |
b | Number or Sprites.Reactive.Value | The value to compare against. |
Returns a read-only Sprites.Reactive.Value, true while a is greater than or equal to b.
const page = Sprites.Reactive.Value(3);
const pages = Sprites.Reactive.Value(5);
const atEnd = Sprites.Maths.GreaterOrEqual(page, pages);
Sprites.Ui.Input.Number(page);
Sprites.Ui.Input.Number(pages);
Sprites.Ui.Dom.Text(' atEnd = ', atEnd);
Sprites.Maths.Greater Sprites.Reactive.Value
Sprites.Maths.Greater(a, b)
True when a is greater than b. Either may be a constant or a reactive value, so the result follows both.
| Input | Type | Description |
|---|---|---|
a | Number or Sprites.Reactive.Value | The value to compare. |
b | Number or Sprites.Reactive.Value | The value to compare against. |
Returns a read-only Sprites.Reactive.Value, true while a is greater than b.
const pages = Sprites.Reactive.Value(3);
const manyPages = Sprites.Maths.Greater(pages, 1);
Sprites.Ui.Input.Number(pages);
Sprites.Ui.Dom.Text(' manyPages = ', manyPages);