Math Class

The Math Class provides a comprehensive set of mathematical functions and constants for a wide range of mathematical operations. It includes well-known mathematical constants such as 'e' (the base of natural logarithms) and 'pi' (the ratio of a circle's circumference to its diameter). The Math Class also offers methods for tasks like rounding numbers, generating random values, and converting angles between degrees and radians.

Constants

Math.E

The constant representing the mathematical constant 'e,' the base of natural logarithms.

Math.PI

The constant representing the mathematical constant 'pi,' the ratio of the circumference of a circle to its diameter.

Math.DEGREES_TO_RADIANS

A constant for converting angles from degrees to radians.

Math.RADIANS_TO_DEGREES

A constant for converting angles from radians to degrees.

Methods

Math.round(a: number): number

Returns the closest long to the argument a, with ties rounding to positive infinity.

var number = 3.7;
var roundedNumber = Math.round(number); // roundedNumber is now 4

Math.random(): Double

Returns a random double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

var randomNumber = Math.random();
// Generates a random number between 0.0 (inclusive) and 1.0 (exclusive)

Math.nextUp(num: number): number

Returns the floating-point value adjacent to the argument num in the direction of positive infinity.

Math.nextUp(1); // returns 1.0000001 

Math.nextDown(num: number): number

Returns the floating-point value adjacent to the argument num in the direction of negative infinity.

Math.nextDown(1); // returns 0.9999999

Math.sqrt(num: number): double

Returns the square root of a number in a double.

var number = 16;
var squareRoot = Math.sqrt(number); // squareRoot is now 4.0