- 4 years ago
- Zaid Bin Khalid
- 5,163 Views
-
6
In this session, you will learn how to perform math operations in JavaScript with the help of an example.
Using the Math Object
To perform mathematical tasks like generating random numbers, rounding numbers, obtaining values such as performing a calculation, PI, and so on, the JavaScript Math object provides several useful properties. It was also useful for performing complex or too complex to perform using standard mathematical operators such as calculating sine or cosine values.
The Math.PI Property
The ratio of the circumference of a circle to its diameter the Math.PI property is used. Approximately 3.14159: Math.PI = 3.14159 is a PI mathematical constant value. As shown in the example.
// Printing PI value
document.write(Math.PI); // Prints: 3.141592653589793
// Function to calculate circle area
function calculateCircleArea(radius){
var area = (Math.PI) * radius * radius;
return area;
}
document.write(calculateCircleArea(5)); // Prints: 78.53981633974483
document.write(calculateCircleArea(10)); // Prints: 314.1592653589793
Getting the Absolute Value
To calculate the absolute (positive) value of a number the Math.abs () method is used that why, -1 is returned as 1, -5 as 5, and so on. As shown in the example
document.write(Math.abs(-1)); // Prints: 1
document.write(Math.abs(1)); // Prints: 1
document.write(Math.abs(-5)); // Prints: 5
document.write(Math.abs(-10.5)); // Prints: 10.5
Generating a Random Number
The method is used to generate a floating-point random number in the range from 0 inclusive up to but not including 1 the method is called Math.random( ). As shown in the example:
document.write(Math.random()); // Expected output: a number between 0 and 1
// Function to create random integer
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
document.write(getRandomInt(3)); // Expected output: 0, 1 or 2
document.write(getRandomInt(1)); // Expected output: 0
Calculating the Square Root of a Number
The method is used to calculate the square root of a number is called The Math.sqrt () =. As shown in the example.
document.write(Math.sqrt(4)); // Prints: 2
document.write(Math.sqrt(16)); // Prints: 4
document.write(Math.sqrt(0.25)); // Prints: 0.5
document.write(Math.sqrt(-9)); // Prints: NaN
/* Function to calculate hypotenuse.
Hypotenuse is the longest side of a right-angled triangle. */
function calculateHypotenuse(a, b) {
return Math.sqrt((a * a) + (b * b));
}
document.write(calculateHypotenuse(3, 4)); // Prints: 5
document.write(calculateHypotenuse(5, 12)); // Prints: 13
Rounding Numbers
The JavaScript Math object provides a few methods and has its purpose to convert the round number.
Round number method:
There is three basic round number method as shown below:
- The Ceil Method.
- The floor Method.
- The Round Method.
Finding the Largest and Smallest Numbers
The method is used to find which number is the largest or smallest in a group of numbers is called the Math.max( ) and Math.min ( ) methods. As shown in the example:
document.write(Math.max(1, 3, 2)); // Prints: 3
document.write(Math.max(-1, -3, -2)); // Prints: -1
document.write(Math.min(1, 3, 2)); // Prints: 1
document.write(Math.min(-1, -3, -2)); // Prints: -3
Raising Numbers to a Power
The method is used to raise a number to a specified power is called Math.pow ( ). The expression is used to check how many times the base x is multiplied by the exponent y is known as Math.pow(x, y). As shown in the example:
document.write(Math.pow(3, 2)); // Prints: 9
document.write(Math.pow(0, 1)); // Prints: 0
document.write(Math.pow(5, -2)); // Prints: 0.04
document.write(Math.pow(16, 0.5)); // Prints: 4 (square root of 4)
document.write(Math.pow(8, 1/3)); // Prints: 2 (cube root of 8)
Performing Trigonometric Operations
To perform trigonometric operations such as sin ( ), cos ( ), tan( ) all these several trigonometric methods provide in the JavaScript’s Math object. As shown in the example:
document.write(Math.sin(0 * Math.PI / 180)); // Prints: 0
document.write(Math.sin(90 * Math.PI / 180)); // Prints: 1
document.write(Math.cos(0 * Math.PI / 180)); // Prints: 1
document.write(Math.cos(180 * Math.PI / 180)); // Prints: -1
document.write(Math.tan(0 * Math.PI / 180)); // Prints: 0
- 4 years ago
- Zaid Bin Khalid
- 5,163 Views
-
6