- Get link
- X
- Other Apps
Mathematical functions or Methods
We need to perform calculations in programs. Thanks to programming languages who supports Math class.Java is one of those languages. We can perform many mathematical operations using java.lang.Math class. It supports all the standard math functions.
In java , we may import Math class using import java.lang.Math; statement to use its methods. All methods in Math class are static,therefore we can use any method by writing Math.MethodName. So, we do not need to import math class.
For example: Math.abs(24)
Mathematical Methods commonly used in Java
Lets discuss the most used method of Math class.
- abs(x): Returns the absolute value of x. For example: abs(29)=29 and abs(-12)=12
- sin(a): Returns the trigonometric sin of angle a in radians. For example: sin(π/2)=1.0
- cos(a): Returns the trigonometric cosine of angle a in radians. For example:cos(π/2)=0.0
- tan(a): Returns the trigonometric tangent of angle a in radians. For example: tan(π/4)=1.0
- asin(b): Returns the arc sine of b. For example: asin(1)=1.57079
- acos(b): Returns the arc cosine of b. For example: acos(-1)=3.14459
- atan(b): Returns the arc tangent of b. For example:atan(1)=0.785398
- ceil(a): Returns the smallest integer that is greater than or equal to parameter a.For example: ceil(7.8)=8.0
- floor(a): Returns the largest integer that is less than or equal to parameter a.For example: floor(7.8)=7.0
- max(x,y): Returns the maximum value of x and y. For example: max(5,10)=10
- min(x,y): Returns the minimum value of x and y. For example: min(5,10)=5
- round(x): Returns the value of x rounded to the nearest whole number.For example: round(7.6)=8 and round(-7.6)=7
- sqrt(x): Returns the square root of x. For example: sqrt(4.0)=2.0
- cbrt(x): Returns the cube root of x. For example: cbrt(27.0)=3.0
- pow(a,b): Returns the number afetr calculating power a for b times.For example: pow(2,3)=8
- exp(a): Returns the natural number (e=2.728) raised to the power of a.For example: exp(2.0)=7.38905
- log(a): Returns the natural logarithm (base e) of a. For example: log(2.7183)=1.0
- random(): Returns the pseudo-random number greater than or equal to 0.0 and less than 1.0. To print random numbers till number n you can multiply by this method by n. For example: random()*9, will print random numbers between 0-9. Random numbers start from 0.
Example: This example will illustrate the use of all the methods given above.
Write a program to print the results of standard methods of Math class in java.
Experiment: Try to calculate different results by passing various values of parameters in the methods.
Keep Learning!Happy Coding!
Comments
Post a Comment
If you have any doubt, ask here