Monday, 12 September 2011

Math class in Java

In this example you will learn about Math class. This example explains how you can use functions provided by the Math class like E, PI, round, abs, ceil, exp, floor, IEEEremainder, max, min, pow, random, rint, sqrt etc. to manipulate the mathematical operation in your program. The Math class is used to operate the calculations. There is not necessary to import any package for the Math class because this is already in java.lang package.
Any expressions can be operated through certain method calls. There are some functions have been used in the given example. All the functions have been explained below with example :
E
This is E field of the Math class which returns you a default exponent value that is closer than any other to e, the base of the natural logarithms.
PI
This is also a field of the Method class which returns you a default pi value, the ratio of the circumference of a circle to its diameter.
abs()
This is the abs() function which returns you the absolute number.
ceil()
This is the ceil() function which returns you the smallest value but greater than the argument.
exp()
This is the exp() function which returns you the exponential value raised to the power of a double value.
floor()
This is the floor() function which returns you the largest value but less than the argument.
IEEEremainder()
This is the IEEEremainder() which returns you the remainder for the given dividend and divisor.
max()
This is the max() function which distinguishes the maximum value from the two given value.
min()
This is the min() function which distinguishes the minimum value from the two given value.
pow()
This is the pow() function which returns you the number raised to the power of a first given value by the another one.
random()
This is the random() function which returns you the random number. It is absolutely system generated.
rint()
This is the rint() function which returns you a value closest to the given value.
round()
This is the round() function which returns you a value that is in the rounded form.
sqrt()
This is the sqrt() function which returns you the square root of the specified value.
Code for the program :
public class mathclass{
  public static void main(String[] args){
  //E and round()
  System.out.println("e = " + Math.round(Math.E*100)/100f);
  //PI
  System.out.println("pi = " + Math.round(Math.PI*100)/100f);
  //abs()
  System.out.println("Absolute number = " + Math.abs(Math.PI));
  //ceil()
  System.out.println("Smallest value but greater than 

the argument = " + Math.ceil(Math.PI));
    //exp()
  System.out.println("Exponent number powered by 

the argument = " + Math.exp(0));
  //floor()
  System.out.println("Largest value but less 

than the argument = " + Math.floor(Math.E));
  //IEEEremainder()
  System.out.println("Remainder = " 

Math.IEEEremainder(5.3f,2.2f));
  //max()
  System.out.println("Maximum Number = " 

Math.max(10,10.3));
  //min()
  System.out.println("Minimum Number = " 

Math.min(10,10.3));
  //pow()
  System.out.println("Power = " + Math.pow(10,3));
  //random()
  System.out.println("Random Number = " 

Math.random());
  //rint()
  System.out.println("Closest to the Argument 

= " + Math.rint(30));
  //round()
  System.out.println("Round = " + Math.round(Math.E));
  //sqrt()
  System.out.println("Square Root = " + Math.sqrt(400));
  }
}
Download Math Class Example

No comments: