Skip to content
bg2 engine

Math Functions

import * as functions from 'bg2e-js/ts/math/functions.ts';
functions.checkPowerOfTwo(1024);

functions.checkPowerOfTwo(n): Tests if a number is a power of two. Returns the number passed if it is greater than EPSILON, or zero otherwise

functions.checkZero(n): Tests if a number is zero, taking into account the constant functions.EPSILON.

functions.equals(n): Tests if a numer is equals to other, taking into account the constant functions.EPSILON.

functions.degreesToRadians(deg) and functions.radiansToDegrees(rad): Converts between radians and degrees.

All the following functions in bg2e math library takes into account the EPSILON constant to round zero values. For example, note the difference between using radiansToDegrees() and RAD_TO_DEG:

import * as mathConsts from 'bg2e-js/ts/math/constants.ts';
import * as functions from 'bg2e-js/ts/math/functions.ts';
const rad = mathConsts.PI_4;
console.log(`${ rad } radians = ${ functions.radiansToDegrees(rad) }º`); // 0.785398163397448 radians = 45º
console.log(`${rad} radians = ${mathConsts.RAD_TO_DEG * rad}º.`); //0.785398163397448 radians = 44.999999999999986º

The fact that the EPSILON constant is taken into account makes the calculations slightly less optimal compared to the use of the native JavaScript mathematical library. For that reason it is advisable to evaluate well which functions we use depending on the case. If we have rounding problems, we can use the bg2 engine mathematical functions, and if we need a little more performance, because we are doing a particularly expensive calculation, it may be better to use the native JavaScript functions.

functions.sin(val)

functions.cos(val)

functions.tan(val)

functions.cotan(val)

functions.atan(val)

functions.atan2(i,j)

functions.seededRandom(): Returns a random number between 0 and 1. This function is responsible for initializing the random number seed using system variable values to improve the randomness of the native Math.random() function.

functions.max(n,b): Returns the higher value between a and b.

functions.min(n,b): Returns the lower value between a and b.

functions.abs(n): Returns the absolute value of n.

functions.sqrt(n): Returns the square root of n.

functions.ler(from, to, t): Performs a linear interpolation between from and to, determined by t (between 0 and 1).

functions.square(n): Returns the square of n (n * n).