Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML

Search:
   Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML Facing Programming Problem? Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
Java - Math class in Java
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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

After downloading the code,in the line nmumber 4, 'Double a....'.In double d should not be in caps. double a = Math.E; is correct.this line is missed in the above code but is available while downloading the code...

regarding this tutorial---great for beginners like me.

Posted by Dinesh on Friday, 04.27.07 @ 10:28am | #15002

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.