# MathUtil Class

Defined in: `js/util/MathUtil.js:9`
Module: util
Parent Module: StructureJS

A helper class to do calculations and conversions.

## Index

Show: Inherited Protected Private Deprecated

### Methods

- constrain
- degreesToRadians
- factorial
- getDivisors
- isEven
- isNegative
- isOdd
- isPositive
- isPrime
- map
- percentToRange
- radiansToDegrees
- randomRange
- rangeToPercent
- sign

## Methods

### `constrain`
(

- `num`
- `min`
- `max`

)
Number

Defined in `js/util/MathUtil.js:22`

Returns a number constrained between min and max.

#### Parameters:

- `num` Number
- `min` Number
- `max` Number

#### Returns:

Number:

#### Example:

     MathUtil.constrain(12, 3, 20);
     // 12

     MathUtil.constrain(22, 3, 20);
     // 20

     MathUtil.constrain(0, 3, 20);
     // 3

### `degreesToRadians`
(

- `degrees`

)
Number

Defined in `js/util/MathUtil.js:160`

Converts degrees to radians.

#### Parameters:

- `degrees` Number

#### Returns:

Number:

#### Example:

     MathUtil.degreesToRadians(90);
     // 1.5707963267948966

     MathUtil.degreesToRadians(180);
     // 3.141592653589793

### `factorial`
(

- `num`

)
Number

Defined in `js/util/MathUtil.js:289`

Calculate the factorial of the integer.

#### Parameters:

- `num` Number

  The number.

#### Returns:

Number:

#### Example:

     MathUtil.factorial(5);
     // 120

     MathUtil.factorial(9);
     // 362880

### `getDivisors`
(

- `num`

)
Array.

Defined in `js/util/MathUtil.js:314`

Return an array of divisors of the integer.

#### Parameters:

- `num` Number

  The number.

#### Returns:

Array.:

#### Example:

### `isEven`
(

- `num`

)
Boolean

Defined in `js/util/MathUtil.js:245`

Check if number is even (convert to Integer if necessary).

#### Parameters:

- `num` Number

  The number.

#### Returns:

Boolean:

#### Example:

     MathUtil.isEven(2);
     // true

     MathUtil.isEven(3);
     // false

### `isNegative`
(

- `num`

)
Boolean

Defined in `js/util/MathUtil.js:211`

Check if number is negative.

#### Parameters:

- `num` Number

  The

#### Returns:

Boolean:

#### Example:

     MathUtil.isNegative(23);
     // false

     MathUtil.isNegative(-23);
     // true

### `isOdd`
(

- `num`

)
Boolean

Defined in `js/util/MathUtil.js:227`

Check if number is odd (convert to Integer if necessary).

#### Parameters:

- `num` Number

  The number.

#### Returns:

Boolean:

#### Example:

     MathUtil.isOdd(2);
     // false

     MathUtil.isOdd(3);
     // true

### `isPositive`
(

- `num`

)
Boolean

Defined in `js/util/MathUtil.js:195`

Check if number is positive (zero is positive).

#### Parameters:

- `num` Number

  The number.

#### Returns:

Boolean:

#### Example:

     MathUtil.isPositive(23);
     // true

     MathUtil.isPositive(-23);
     // false

### `isPrime`
(

- `num`

)
Boolean

Defined in `js/util/MathUtil.js:263`

Check if number is Prime (divisible only by itself and one).

#### Parameters:

- `num` Number

  The number.

#### Returns:

Boolean:

#### Example:

     MathUtil.isPrime(4);
     // false

     MathUtil.isPrime(5);
     // true

### `map`
(

- `num`
- `min1`
- `max1`
- `min2`
- `max2`

)
Number

Defined in `js/util/MathUtil.js:112`

Re-maps a number from one range to another. The output is the same as inputing the result of rangeToPercent() numbero percentToRange(). Example: num = 10, min1 = 0, max1 = 100, min2 = 0, max2 = 50 // outputs 5

#### Parameters:

- `num` Number
- `min1` Number
- `max1` Number
- `min2` Number
- `max2` Number

#### Returns:

Number:

#### Example:

     MathUtil.map(10, 0, 100, 0, 50);
     // 5

### `percentToRange`
(

- `percent`
- `min`
- `max`

)
Number

Defined in `js/util/MathUtil.js:96`

Returns the number that corresponds to the percentage in a given range. Example: percent = 0.5 range 10 to 20 // outputs 15

#### Parameters:

- `percent` Number
- `min` Number
- `max` Number

#### Returns:

Number:

#### Example:

     MathUtil.percentToRange(0.5, 10, 20);
     // 15

### `radiansToDegrees`
(

- `radians`

)
Number

Defined in `js/util/MathUtil.js:144`

Converts radians to degrees.

#### Parameters:

- `radians` Number

#### Returns:

Number:

#### Example:

     MathUtil.radiansToDegrees(1.5707963267948966);
     // 90

     MathUtil.radiansToDegrees(3.141592653589793);
     // 180

### `randomRange`
(

- `min`
- `max`
- `[wholeNumber=true]`

)
Number

Defined in `js/util/MathUtil.js:51`

Returns a random number between min and max.

#### Parameters:

- `min` Number
- `max` Number
- `[wholeNumber=true]` Number optional

#### Returns:

Number:

#### Example:

### `rangeToPercent`
(

- `num`
- `min`
- `max`
- `constrainMin`
- `constrainMax`

)
Number

Defined in `js/util/MathUtil.js:70`

Returns the percentage of a number in a given range. Example: num = 15 range 10 to 20 // outputs 0.5

#### Parameters:

- `num` Number
- `min` Number
- `max` Number
- `constrainMin` Boolean

  Returns 0 if num < min.

- `constrainMax` Boolean

  Returns 1 if num > max.

#### Returns:

Number:

#### Example:

     MathUtil.rangeToPercent(15, 10, 20);
     // 0.5

### `sign`
(

- `num`

)
Number

Defined in `js/util/MathUtil.js:176`

Returns 1 if the value is >= 0. Returns -1 if the value is < 0.

#### Parameters:

- `num` Number

#### Returns:

Number:

#### Example:

     MathUtil.sign(23);
     // 1

     MathUtil.sign(-23);
     // -1

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index