# base/math (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/math/

Math functions. (interpolation, clamping, rounding, trigonometry, random numbers, following)
- Importing the engine's Math hides the JavaScript built-in Math under that name.

Example: Convert angles, clamp, interpolate, roll a die

```js
import { Math } from "./build/vanilla.js";

const radian = Math.degreeToRadian(90);
const sine = Math.sin(radian);
const clamped = Math.clamp(150, 0, 100);
const middle = Math.lerp(0, 10, 0.5);
const dice = Math.randomInt(1, 6);
console.log(sine, clamped, middle, dice);
```

## Functions and constants

- `PI = 3.141592653589793` (field, since 0.0.8): Returns pi.
  - returns number
- `PositiveInfinity = Infinity` (field, since 0.0.8): Returns the maximum value.
  - returns number
- `NegativeInfinity = -Infinity` (field, since 0.0.8): Returns the minimum value.
  - returns number
- `min(left, right)` (method, since 0.0.8): Returns the lower number.
  - `left` number: First number to compare.
  - `right` number: Second number to compare.
  - returns number: The smaller of the two.
- `max(left, right)` (method, since 0.0.8): Returns the higher number.
  - `left` number: First number to compare.
  - `right` number: Second number to compare.
  - returns number: The larger of the two.
- `clamp(value, minValue, maxValue)` (method, since 0.0.8): Clamps to a range.
  - `value` number: Value to clamp.
  - `minValue` number: Lower bound.
  - `maxValue` number: Upper bound. (if it is below minValue, minValue wins)
  - returns number: The value clamped to minValue ~ maxValue inclusive.
- `clamp01(value)` (method, since 0.0.9): Clamps to a range.
  - `value` number: Value to clamp.
  - returns number: The value clamped to 0 ~ 1 inclusive.
- `lerp(source, destination, normalizedTime)` (method, since 0.0.8): Linear interpolation.
  - `source` number: Start value. (when normalizedTime is 0)
  - `destination` number: End value. (when normalizedTime is 1)
  - `normalizedTime` number: Interpolation ratio. (0 ~ 1, not clamped, so values outside extrapolate beyond)
  - returns number: The interpolated value.
- `random()` (method, since 0.0.8): Returns a random value. (0 ~ 1)
  - returns number: A random number from 0 inclusive to 1 exclusive.
- `floor(value)` (method, since 0.0.8): Returns the floor value.
  - `value` number: Number to round down.
  - returns number: The largest integer less than or equal to value. (infinity and NaN are returned as is)
- `ceil(value)` (method, since 0.0.8): Returns the ceiling value.
  - `value` number: Number to round up.
  - returns number: The smallest integer greater than or equal to value. (infinity and NaN are returned as is)
- `round(value)` (method, since 0.0.8): Returns the rounded value.
  - `value` number: Number to round.
  - returns number: The nearest integer. (.5 goes up, so -2.5 becomes -2, infinity and NaN are returned as is)
- `sqrt(value)` (method, since 0.0.8): Returns the square root.
  - `value` number: Number to take the square root of.
  - returns number: Square root. (NaN for negative numbers)
- `abs(value)` (method, since 0.0.8): Returns the absolute value.
  - `value` number: Number to take the absolute value of.
  - returns number: Absolute value.
- `sin(value)` (method, since 0.0.8): Returns the sine.
  - `value` number: Angle. (radians)
  - returns number: Sine. (the browser Math.sin, correctly rounded, so it has the same bits as the Rust engine)
- `cos(value)` (method, since 0.0.8): Returns the cosine.
  - `value` number: Angle. (radians)
  - returns number: Cosine. (the browser Math.cos, correctly rounded, so it has the same bits as the Rust engine)
- `tan(value)` (method, since 0.0.8): Returns the tangent.
  - `value` number: Angle. (radians)
  - returns number: Tangent. (the browser Math.tan, correctly rounded, so it has the same bits as the Rust engine)
- `asin(value)` (method, since 0.0.8): Returns the arcsine.
  - `value` number: Sine value. (-1 ~ 1)
  - returns number: Arcsine. (radians, NaN if out of range)
- `acos(value)` (method, since 0.0.8): Returns the arccosine.
  - `value` number: Cosine value. (-1 ~ 1)
  - returns number: Arccosine. (radians, NaN if out of range)
- `pow(base, exponent)` (method, since 0.0.8): Returns the power.
  - `base` number: Base.
  - `exponent` number: Exponent.
  - returns number: base raised to the power of exponent.
- `degreeToRadian(degree)` (method, since 0.0.8): Converts degrees to radians and returns the result.
  - `degree` number: Angle. (degrees)
  - returns number: The angle in radians.
- `radianToDegree(radian)` (method, since 0.0.8): Converts radians to degrees and returns the result.
  - `radian` number: Angle. (radians)
  - returns number: The angle in degrees.
- `randomRange(minValue, maxValue)` (method, since 0.3.0): Returns a random real number from min (inclusive) to max (exclusive).
  - `minValue` number: Lower bound. (inclusive)
  - `maxValue` number: Upper bound. (exclusive)
  - returns number: A real number from minValue inclusive to maxValue exclusive.
- `randomInt(minValue, maxValue)` (method, since 0.3.0): Returns a random integer from min to max (both inclusive).
  - `minValue` number: Lower bound. (integer, inclusive)
  - `maxValue` number: Upper bound. (integer, inclusive)
  - returns number: An integer from minValue to maxValue inclusive.
- `pickRandom(array)` (method, since 0.3.0): Picks one element from an array at random and returns it.
  - `array` T[]: Array to pick from.
  - returns T | undefined: The picked item. (undefined if the array is empty or missing)
- `shuffle(array, randomFunction = random)` (method, since 0.3.0): In-place array shuffle. (Fisher-Yates)
  - If randomFunction is passed, uses that random source. (SeededRandom's nextValue, etc.)
  - `array` T[]: Array to shuffle. (changed in place)
  - `randomFunction` Function: A function that returns a value from 0 (inclusive) to 1 (exclusive).
  - returns T[]: The same array, shuffled.

  Example: Always shuffle in the same order with a seeded random

  ```js
  import { Math, SeededRandom } from "./build/vanilla.js";
  
  const seededRandom = new SeededRandom(7);
  const cardList = [1, 2, 3, 4, 5];
  Math.shuffle(cardList, () => seededRandom.nextValue());
  console.log(cardList);
  ```

- `approach(currentValue, targetValue, rate, timeDelta)` (method, since 0.3.0): Frame-rate-independent exponential decay follow.
  - Follows smoothly without jumping even if the target changes midway. (A formula several games kept reinventing)
  - The larger rate is, the faster it catches up. current + (target - current) * (1 - e^(-rate * dt))
  - `currentValue` number: Current value.
  - `targetValue` number: Target value.
  - `rate` number: Following speed. (per second, 0 or more, larger catches up faster)
  - `timeDelta` number: Interval since the last frame. (seconds)
  - returns number: The value moved toward the target. (does not overshoot the target)

  Example: Smoothly follow a target with a camera

  ```js
  import { Math } from "./build/vanilla.js";
  
  const targetX = 100;
  const timeDelta = 1 / 60;
  let cameraX = 0;
  for (let frame = 0; frame < 60; ++frame) {
  	cameraX = Math.approach(cameraX, targetX, 5, timeDelta);
  }
  console.log(cameraX);
  ```

- `moveTowards(currentValue, targetValue, maxDelta)` (method, since 0.3.0): Moves a value toward a target at a constant speed. (Does not overshoot)
  - `currentValue` number: Current value.
  - `targetValue` number: Target value.
  - `maxDelta` number: Maximum amount to move at once. (0 or more, multiply a per-second speed by timeDelta before passing it)
  - returns number: The value moved toward the target by at most maxDelta. (does not overshoot the target)

  Example: Move to a target at a constant speed

  ```js
  import { Math } from "./build/vanilla.js";
  
  const speed = 4;
  const timeDelta = 0.5;
  let value = 0;
  for (let step = 0; step < 4; ++step) {
  	value = Math.moveTowards(value, 5, speed * timeDelta);
  	console.log(value);
  }
  ```

- `easeOutQuad(time)` (method, since 0.4.0): Easing: quadratic curve decelerating outward. (0 gives 0, 1 gives 1)
  - `time` number: Progress ratio. (0 ~ 1)
  - returns number: The ratio after the deceleration curve. (0 ~ 1)
