TypeScript math object:
A TypeScript Math object provides no. of properties and methods for performing mathematical operations. Math is not a constructor and all the properties and methods of Math are static.
TypeScript Math Object Properties:
Property |
Description |
E \ |
Euler’s constant and the base of natural logarithms, approximately 2.718. |
LN2 |
Natural logarithm of 2, approximately 0.693. |
LN10 |
Natural logarithm of 10, approximately 2.302. |
LOG2E |
Base 2 logarithm of E, approximately 1.442. |
LOG10E |
Base 10 logarithm of E, approximately 0.434. |
PI |
Ratio of the circumference of a circle to its diameter, approximately 3.14159. |
SQRT1_2 |
Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707. |
SQRT2 |
Square root of 2, approximately 1.414. |
TypeScript Math Object Methods:
Method |
Description |
abs() |
It returns the absolute value of a number. |
acos() |
It returns the arccosine (in radians) of a number. |
asin() |
It returns the arcsine (in radians) of a number. |
atan() |
It returns the arctangent (in radians) of a number. |
atan2() |
It returns the arctangent of the quotient of its arguments. |
ceil() |
It returns the smallest integer greater than or equal to a number. |
cos() |
It returns the cosine of a number. |
exp() |
It returns EN, where N is the argument, and E is Euler’s constant, the base of the natural logarithm. |
floor() |
It returns the largest integer less than or equal to a number. |
log() |
It returns the natural logarithm (base E) of a number. |
max() |
It returns the largest of zero or more numbers. |
min() |
It returns the smallest of zero or more numbers. |
pow() |
It returns base to the exponent power, that is, base exponent. |
random() |
It returns a pseudo-random number between 0 and 1. |
round() |
It returns the value of a number rounded to the nearest integer. |
sin() |
It returns the sine of a number. |
sqrt() |
It returns the square root of a number. |
tan() |
It returns the tangent of a number. |
toSource() |
It returns the string “Math”. |
Example:
function mathTest(num:number):void{
var squarRoot = Math.sqrt(num);
console.log("Random Number: " + num);
console.log("Square root: " + squarRoot);
}
var num:number = Math.random();
mathTest(num);