Home Java Beginners Java - Math class in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java - Math class in Java

Advertisement
In this example you will learn about Math class.

Java - 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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2007

Related Tutorials

Ask Questions?    Discuss: Java - Math class in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
lexibelle
September 6, 2011
yiee

tnx 4 d help i hope u can help more people around the globe yiee let's s.o.p
HOANGNI
October 28, 2011
PHAN MEM MATHCLSS

KHONG HIEU
SINTHA MATHAR.k
July 16, 2013
mathar

command click
Chetan Rawal
September 17, 2013
Java Math class is limited

Although the methods here could allow mathematical manipulations in Java, I just find using math software like MATLAB work better for real math computations. Especially given the fact that you can "embed" (in a crude sense of the word) MATLAB algorithms as is within the Java environment ( see http://www.mathworks.com/discovery/java-math.html )
DMCA.com