# Math reference

## **Operators**

## **Arithmetic**

2 + 3; // result: 5 2 - 3; // result: -1 4 \* 5; // result: 20 6 / 3; // result: 2

## **Modulo**

The modulo operator (represented as `%`) is used to divide into a number and get the remainder.

9 % 4; // result: 1 8 % 4; // result: 0 6.5 % 2.5; // result: 1.5

## **Math functions**

All of the functions in the javascript `Math` object are available. You don’t have to type `Math.abs(-2)`, you can just type `abs(-2)`. For example:

sqrt(2); // result: 1.4142… abs(-3); // result: 3 floor(2.3); // result: 2 ceil(2.3); // result: 3 max(1, 5, 2); // result: 5 min(1, 5, 2); // result: 1

These functions will also work component-wise with [vectors](https://cuttle.xyz/learn/parameters/vector-reference).

## **Trigonometry uses degrees**

All the top-level trigonometry functions use degrees, not radians. For example:

sin(90); // result: 1 cos(30); // result: 0.8660… acos(0.75); // result: 41.4096…

If you’d like to use radians, you can use e.g. `Math.sin` instead of `sin`.

## **Additional functions**

`mix(a, b, t)` blends between `a` and `b` as `t` goes from `0` to `1`.

mix(4, 8, 0); // result: 4 mix(4, 8, 0.5); // result: 6 mix(4, 8, 1); // result: 8

`clamp(x, low, high)` constrains `x` between `low` and `high`

clamp(-4, 0, 2); // result: 0 clamp(8, 0, 2); // result: 2 clamp(1.5, 0, 2); // result: 1.5

`angularDistance(a, b)` will return the smallest absolute difference between angles `a` and `b`. It will be in the range `0` to `180`.

angularDistance(10, 350); // result: 20

`modulo(x, base)` is like the modulo `%` operator except if `x` is negative it will return a positive result.

modulo(-2, 5) - // result: 3 (2 % 5); // result: -2

`moduloDistance(a, b, base)` returns the smallest absolute difference between `a` and `b` in modulo `base` space. The result will be constrained to `0 <= difference <= base / 2`. It’s a generalization of `angularDistance`.

moduloDistance(2, 8, 10); // result: 4

## **Random**

Although you can use `Math.random()` the result will jitter as it generates a new random value every time.

Instead, we recommend using `random()` which won’t jitter.

random(); // A random number between 0 and 1 // e.g. 0.2999… random(5); // A random number between 0 and 5 // e.g. 1.4993… random(3, 5); // A random number between 3 and 5 // e.g. 3.5997…

`randomInt` is like `random` but will only return integers

randomInt(10); // A random integer between 0 and 10 (inclusive) // e.g. 4 randomInt(3, 5); // A random integer between 3 and 5 (inclusive) // e.g. 4

There are also functions for generating random [vectors](https://cuttle.xyz/learn/parameters/vector-reference).

randomDirection(); // A random vector of unit length // e.g. Vec(-0.3082…, 0.9513…) randomDirection(10); // A random vector of length 10 // e.g. Vec(-3.0822…, 9.5132…) randomPointInDisk(); // A random vector contained within a circle of radius 1 // e.g. Vec(-0.5466…, 0.0334…) randomPointInDisk(10); // A random vector contained within a circle of radius 10 // e.g. Vec(-5.4658…, 0.3345…)

## **Seeded random generator**

If you need to generate the same sequence of random numbers, generated by a _seed_, you can do so by creating a `new RandomGenerator(seed)`.

const rng = new RandomGenerator(324309); rng.random(); // result: 0.6108…

The random generator has as methods all of the above random functions.