Java Math Abs() Round() Ceil() Floor() Min() Methods with Example

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.
In this tutorial, you will learn:
  • Math.abs
  • Math.round
  • Math.ceil & Math.floor
  • Math.min
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.
Java Math Class Tutorial
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
MethodDescriptionArguments
absReturns the absolute value of the argumentDouble, float, int, long
roundReturns the closed int or long (as per the argument)double or float
ceilReturns the smallest integer that is greater than or equal to the argumentDouble
floorReturns the largest integer that is less than or equal to the argumentDouble
minReturns the smallest of the two argumentsDouble, float, int, long
maxReturns the largest of the two argumentsDouble, float, int, long
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;

Math.abs

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.45

Math.round

public class Guru99 {
 public static void main(String args[]) {
  double d1 = 84.6;
  double d2 = 0.45;
  System.out.println("Round off for d1: " + Math.round(d1));

  System.out.println("Round off for d2: " + Math.round(d2));
 }
}

Output:
Round off for d1: 85
Round off for d2: 0

Math.ceil & Math.floor

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));

 }
}
Output:
Ceiling of '84.6' = 85.0
Floor of '84.6' = 84.0
Ceiling of '0.45' = 1.0
Floor of '0.45' = 0.0

Math.min

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.6
B) Let us have a look at the table below that shows us the Exponential and Logarithmic methodsand its description-
MethodDescriptionArguments
expReturns the base of natural log (e) to the power of argumentDouble
LogReturns the natural log of the argumentdouble
PowTakes 2 arguments as input and returns the value of the first argument raised to the power of the second argumentDouble
floorReturns the largest integer that is less than or equal to the argumentDouble
SqrtReturns the square root of the argumentDouble
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.0
C) Let us have a look at the table below that shows us the Trigonometric methods and its description-
MethodDescriptionArguments
SinReturns the Sine of the specified argumentDouble
CosReturns the Cosine of the specified argumentdouble
TanReturns the Tangent of the specified argumentDouble
Atan2Converts rectangular coordinates (x, y) to polar(r, theta) and returns thetaDouble
toDegreesConverts the arguments to degreesDouble
SqrtReturns the square root of the argumentDouble
toRadiansConverts the arguments to radiansDouble
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.1071487177940904
Now, with the above, you can also design your own scientific calculator in java.

No comments:

Post a Comment