Java has had several advanced usage application including working with complex calculations in physics, architecture/designing of structures, working with Maps and corresponding latitudes/longitudes, etc. Show In this Java tutorial, you will learn: All such applications require using complex calculations/equations that are tedious to perform manually. Programmatically, such calculations would involve usage of logarithms, trigonometry, exponential equations, etc. Now, you cannot have all the log or trigonometry tables hard-coded somewhere in your application or data. The data would be enormous and complex to maintain. Java provides a very useful class for this purpose. It is the Math java class (java.lang.Math). This class provides methods for performing the operations like exponential, logarithm, roots and trigonometric equations too. Let us have a look at the methods provided by the Java Math class. The two most fundamental elements in Math are the ‘e’ (base of the natural logarithm) and ‘pi’ (ratio of the circumference of a circle to its diameter). These two constants are often required in the above calculations/operations. Hence the Math class java provides these two constants as double fields. Math.E – having a value as 2.718281828459045 Math.PI – having a value as 3.141592653589793 A) Let us have a look at the table below that shows us the Basic methods and its description
Below is the code implementation of the above methods: Note: There is no need to explicitly import java.lang.Math as its imported implicitly. All its methods are static. Integer Variable int i1 = 27; int i2 = -45;Double(decimal) variables double d1 = 84.6; double d2 = 0.45;Java Math abs() method with ExampleJava Math abs() method returns the absolute value of the argument. public class Guru99 { public static void main(String args[]) { int i1 = 27; int i2 = -45; double d1 = 84.6; double d2 = 0.45; System.out.println("Absolute value of i1: " + Math.abs(i1)); System.out.println("Absolute value of i2: " + Math.abs(i2)); System.out.println("Absolute value of d1: " + Math.abs(d1)); System.out.println("Absolute value of d2: " + Math.abs(d2)); } }Output: Absolute value of i1: 27 Absolute value of i2: 45 Absolute value of d1: 84.6 Absolute value of d2: 0.45Java Math.round() method with ExampleMath.round() method in Java returns the closed int or long as per the argument. Below is the example of math.round Java method. Output: Round off for d1: 85 Round off for d2: 0Java Math.ceil and Math.floor method with ExampleThe Math.ceil and Math.floor in Java methods are used to return the smallest and largest integer that are greater than or equal to the argument. Below is the Math floor and ceiling Java example. public class Guru99 { public static void main(String args[]) { double d1 = 84.6; double d2 = 0.45; System.out.println("Ceiling of '" + d1 + "' = " + Math.ceil(d1)); System.out.println("Floor of '" + d1 + "' = " + Math.floor(d1)); System.out.println("Ceiling of '" + d2 + "' = " + Math.ceil(d2)); System.out.println("Floor of '" + d2 + "' = " + Math.floor(d2)); } }We will get the below output of the math.ceil in Java example. Output: Ceiling of '84.6' = 85.0 Floor of '84.6' = 84.0 Ceiling of '0.45' = 1.0 Floor of '0.45' = 0.0Java Math.min() method with ExampleThe Java Math.min() method returns the smallest of the two arguments. public class Guru99 { public static void main(String args[]) { int i1 = 27; int i2 = -45; double d1 = 84.6; double d2 = 0.45; System.out.println("Minimum out of '" + i1 + "' and '" + i2 + "' = " + Math.min(i1, i2)); System.out.println("Maximum out of '" + i1 + "' and '" + i2 + "' = " + Math.max(i1, i2)); System.out.println("Minimum out of '" + d1 + "' and '" + d2 + "' = " + Math.min(d1, d2)); System.out.println("Maximum out of '" + d1 + "' and '" + d2 + "' = " + Math.max(d1, d2)); } }Output: Minimum out of '27' and '-45' = -45 Maximum out of '27' and '-45' = 27 Minimum out of '84.6' and '0.45' = 0.45 Maximum out of '84.6' and '0.45' = 84.6B) Let us have a look at the table below that shows us the Exponential and Logarithmic methods and its description-
Below is the code implementation of the above methods: (The same variables are used as above) public class Guru99 { public static void main(String args[]) { double d1 = 84.6; double d2 = 0.45; System.out.println("exp(" + d2 + ") = " + Math.exp(d2)); System.out.println("log(" + d2 + ") = " + Math.log(d2)); System.out.println("pow(5, 3) = " + Math.pow(5.0, 3.0)); System.out.println("sqrt(16) = " + Math.sqrt(16)); } }Output: exp(0.45) = 1.568312185490169 log(0.45) = -0.7985076962177716 pow(5, 3) = 125.0 sqrt(16) = 4.0C) Let us have a look at the table below that shows us the Trigonometric methods and its description-
Default Arguments are in Radians Below is the code implementation: public class Guru99 { public static void main(String args[]) { double angle_30 = 30.0; double radian_30 = Math.toRadians(angle_30); System.out.println("sin(30) = " + Math.sin(radian_30)); System.out.println("cos(30) = " + Math.cos(radian_30)); System.out.println("tan(30) = " + Math.tan(radian_30)); System.out.println("Theta = " + Math.atan2(4, 2)); } }Output: sin(30) = 0.49999999999999994 cos(30) = 0.8660254037844387 tan(30) = 0.5773502691896257 Theta = 1.1071487177940904Now, with the above, you can also design your own scientific calculator in java.
The Math.ceil() function always rounds up and returns the smaller integer greater than or equal to a given number. The smallest integer greater than or equal to x. It's the same value as -Math.floor(-x). Because ceil() is a static method of Math, you always use it as Math.ceil(), rather than as a method of a Math object you created (Math is not a constructor). Math.ceil(-Infinity); Math.ceil(-7.004); Math.ceil(-4); Math.ceil(-0.95); Math.ceil(-0); Math.ceil(0); Math.ceil(0.95); Math.ceil(4); Math.ceil(7.004); Math.ceil(Infinity);
BCD tables only load in the browser
|