. Advertisement .
..3..
. Advertisement .
..4..
Java provides us with advanced calculation support functions such as sine, cosine, square root, rounding,… Good use of this class makes our programming simpler. The following article about “Summary of basic math functions in Javascript” will help you better understand math functions in Java.
What are math functions?
When one or more independent variables’ values are known, a mathe function is a rule that establishes the value of the dependent variable in respect to those variables. A function can be represented in a number of ways, such as a formula, a table, or a graph. Excepting for isolated points, all the math functions in physical chemistry are only valued. Excepting for insulated areas, the math functions that develop in physical chemistry are continuing. Both the quantum mechanical theory and the thermodynamic theory define the number of independent variables for the functions that take place in these disciplines.
Basic math functions in Java
The math functions (Math) are located in the <math.h>, <stdlib.h> and <complex.h> libraries. Below are some basic math functions in Java:
1. Math.abs()
The absolute value of the parameter provided to the math.abs() method is returned. The parameter’s positive absolute value is its value. The positive value that corresponds to the negative value without a sign is returned if the parameter value is negative. Here is an instance of the math.abs() method:
int abs1 = Math.abs(10); // abs1 = 10
int abs2 = Math.abs(-20); // abs2 = 20
Parameters can be int, float, long, double.
Math.abs(int)
Math.abs(long)
Math.abs(float)
Math.abs(double)
2. Math.ceil()
A floating point number is rounded up to the nearest integer value using the Math.ceil() function. A double is returned in place of the rounded value. Here is a Java example of Math.ceil():
double ceil = Math.ceil(7.343); // ceil = 8.0
3. Math.floor ()
Math.floor() returns double which is the number rounded down to the nearest integer value.
Here is an example:
double floor = Math.floor(7.343); // floor = 7.0
4. Math.floorDiv ()
It is the division of two integers, the result is rounded down to the nearest integer.However there will be a difference with the result of the division calculation in math. Here’s an example so you can tell the difference:
Input:
double result3 = Math.floorDiv(-100,9);
System.out.println("result3: " + result3);
double result4 = -100 / 9;
System.out.println("result4: " + result4);
Output
result3: -12.0
result4: -11.0
5. Math.min()
Use this math function to find the smallest parameter. The example below after applying will return the result of 10
int min = Math.min(10, 20);
6. Math.max()
In contrast to the Math.min() function, if you want to find the maximum value use this function Math.max(), the example below gives a result is 20.
int max = Math.max(10, 20);
7. Math.round()
Math.round() rounds numbers according to the usual rules in math. For example:
double roundedDown = Math.round(23.445);
double roundedUp = Math.round(23.545);
The result is 23 and 24 respectively.
8. Math.random()
Math.random() returns a float-point between 0 and 1. If you want to randomize a random integer between 0 and 100 you can combine Math.random() with round(), ceil(), floor( ).
For example:
double random = Math.random();
double random = Math.random() * 100D;
Some familiar exponential and logarithmic math functions
Math class also contains a set of methods that support exponential and Logarithmic.
1. Math.exp ()
Math.exp() returns the exponent n in the argument passed to the constant e.
Here is the input:
double exp1 = Math.exp(1);
System.out.println("exp1 = " + exp1);
double exp2 = Math.exp(2);
System.out.println("exp2 = " + exp2);
The result:
exp1 =
2.718281828459045 exp2 = 7.38905609893065
2. Math.log ()
Math.log() returns the base e logarithm of n as the input parameter.
Such as:
double log1 = Math.log(1);
System.out.println("log1 = " + log1);
double log10 = Math.log(10);
System.out.println("log10 = " + log10);
Result:
log1 = 0.0
log10 = 2.302585092994046
3. Math.log10 ()
Math.log10() returns the base 10 logarithm of n as the input parameter. For example:
double log10_1 = Math.log10(1);
System.out.println("log10_1 = " + log10_1);
double log10_100 = Math.log10(100);
System.out.println("log10_100 = " + log10_100);
Result:
log10_1 = 0.0
log10_100 = 2.0
4. Math.pow ()
Math.pow(double t1, double t2) accepts two parameters t1, t2, where t1 is a number that is powered to t2 times. For example:
double pow2 = Math.pow(2,2);
System.out.println("pow2 = " + pow2);
double pow8 = Math.pow(2,8);
System.out.println("pow8 = " + pow8);
Result:
pow2 = 4,0
pow8 = 256,0
5. Math.sqrt ()
Math.sqrt() returns the square root of the input parameter. For example:
double sqrt4 = Math.sqrt(4);
System.out.println("sqrt4 = " + sqrt4);
double sqrt9 = Math.sqrt(9);
System.out.println("sqrt9 = " + sqrt9);
Result:
sqrt4 = 2.0
sqrt9 = 3.0
Trigonometric Math Functions in Java
1. Math.PI
Math.PI is a constant of type double in the Math class that holds the value of the mathematical constant PI.
2. Math.sin ()
Math.sin() returns the sine of an angle in radians. Note that the formula for converting from degrees to radians is (degrees / 180 * PI). We can use toRadians() to convert degrees to radians. For example:
double sin = Math.sin(Math.PI);
System.out.println("sin = " + sin);
3. Math.cos ()
Math.cos() returns the cosine of an angle in radians.
For example:
double cos = Math.cos(Math.PI);
System.out.println("cos = " + cos);
4. Math.tan()
Math.tan() returns the tan value of an angle in radians.
For example:
double tan = Math.tan(Math.PI);
System.out.println("tan = " + tan);
5. Math.asin()
Math.asin() returns the arc sine value. For example:
double asin = Math.asin(1.0);
System.out.println("asin = " + asin);
6. Math.acos()
Math.acos() returns the arc cose value. For example:
double acos = Math.acos(1.0);
System.out.println("acos = " + acos);
7. Math.atan()
Math.atan() returns the arc tane value. For example:
double atan = Math.atan(1.0);
System.out.println("atan = " + atan);
8.Math.atan2()
This is additional information for introduction, you can learn more in the Java documentation.
9.Math.sinh ()
Returns the hyperbolic sine value. For example:
double sinh = Math.sinh(1.0);
System.out.println("sinh = " + sinh);
10. Math.cosh ()
Returns the hyperbolic cose value. For example:
double cosh = Math.cosh(1.0);
System.out.println("cosh = " + cosh);
11.Math.tanh ()
Returns the hyperbolic tane value.For example:
double tanh = Math.tanh(1.0);
System.out.println("tanh = " + tanh);
12. Math.toDegrees()
Math.toDegrees() converts radians to degrees. For example:
double degrees = Math.toDegrees(Math.PI);
System.out.println("degrees = " + degrees);
13. Math.toRadians()
Math.toRadians() Converts degrees to radians.For example:
double radians = Math.toRadians(180);
System.out.println("radians = " + radians);
Conclusion
Above is a summary of “Basic Math Functions” we would like to introduce to you. Hope you will soon get acquainted with functions in Java. Thank for reading!
Leave a comment