Constants And Basic Methods of Math Class in Java


Introduction

The Java programming language supports basic arithmetic with its arithmetic operators: +, -, *, /, and %.

The Math class in the java.lang package provides methods and constants for doing more advanced mathematical computation.

The methods in the Math class are all static, so you call them directly from the Math class.

Constants in Math Class

Math.E is the base of natural logarithms.

Math.E = 2.718281828459045

Math.PI is the ratio of the circumference of a circle to its diameter.

Math.PI = 3.141592653589793

Basic Methods in Math Class

Math class includes more than 40 static methods.
Below are some basic methods.


Returns the absolute value of the argument.

double abs(double d)
float abs(float f)
int abs(int i)
long abs(long lng)

Math.abs(7.8) = 7.8
Math.abs(-9) = 9

Absolute value From Wikipedia

In mathematics, the absolute value is the non-negative value of x without regard to its sign.
For example, the absolute value of 3 is 3, and the absolute value of −3 is also 3.
The absolute value of a number may be thought of as its distance from zero.

Returns the smallest integer that is greater than or equal to the argument. Returned as a double.

double ceil(double d)

Math.ceil(1.3) = 2.0
Math.ceil(-9.7) = -9.0

ceil : the upper interior surface of a room or other similar compartment.

Returns the largest integer that is less than or equal to the argument. Returned as a double.

double floor(double d)

Math.floor(1.3) = 1.0
Math.floor(-9.7) = -10.0

Returns the integer that is closest in value to the argument. Returned as a double.

double rint(double d)

Math.rint(1.3) = 1.0
Math.rint(-9.7) = -10.0
Math.rint(2.5) = 2.0

Returns the closest long or int, as indicated by the method's return type, to the argument.

long round(double d)
int round(float f)

Math.round(2.587) = 3
Math.round(-9.132) = -9
Math.round(5.7F) = 6

Returns the smaller of the two arguments.

double min(double arg1, double arg2)
float min(float arg1, float arg2)
int min(int arg1, int arg2)
long min(long arg1, long arg2)

Math.min(5.7F,1.7F) = 1.7
Math.min(6,9) = 6

Returns the larger of the two arguments.

double max(double arg1, double arg2)
float max(float arg1, float arg2)
int max(int arg1, int arg2)
long max(long arg1, long arg2)

Math.max(5.7F,1.7F) = 5.7
Math.max(6,9) = 9

Sample Code of Constants and Basic Methods about Math class in Java

public class main {

public static void main (String [] args)
{
//Math.E, which is the base of natural logarithms.
System.out.println("Math.E = "+ Math.E);
//Math.E = 2.718281828459045

//Math.PI, which is the ratio of the circumference of a circle to its diameter.
System.out.println("Math.PI = "+ Math.PI);
//Math.PI = 3.141592653589793

//Returns the absolute value of the argument
System.out.println("Math.abs(7.8) = "+ Math.abs(7.8));
//Math.abs(7.8) = 7.8

System.out.println("Math.abs(-9) = "+ Math.abs(-9));
//Math.abs(-9) = 9

//Returns the smallest integer that is greater than or equal to the argument.
//Returned as a double.
System.out.println("Math.ceil(1.3) = "+ Math.ceil(1.3));
//Math.ceil(1.3) = 2.0

System.out.println("Math.ceil(-9.7) = "+ Math.ceil(-9.7));
//Math.ceil(-9.7) = -9.0

//Returns the largest integer that is less than or equal to the argument.
//Returned as a double.
System.out.println("Math.floor(1.3) = "+ Math.floor(1.3));
//Math.floor(1.3) = 1.0

System.out.println("Math.floor(-9.7) = "+ Math.floor(-9.7));
//Math.floor(-9.7) = -10.0

//Returns the integer that is closest in value to the argument.
//Returned as a double.
System.out.println("Math.rint(1.3) = "+ Math.rint(1.3));
//Math.rint(1.3) = 1.0

System.out.println("Math.rint(-9.7) = "+ Math.rint(-9.7));
//Math.rint(-9.7) = -10.0

System.out.println("Math.rint(2.5) = "+ Math.rint(2.5));
//Math.rint(2.5) = 2.0

//Returns the closest long or int, as indicated by the method's return type, to the argument.
System.out.println("Math.round(2.587) = "+ Math.round(2.587));
//Math.round(2.587) = 3

System.out.println("Math.round(-9.132) = "+ Math.round(-9.132));
//Math.round(-9.132) = -9

System.out.println("Math.round(5.7F) = "+ Math.round(5.7F));
//Math.round(5.7F) = 6

//Returns the smaller of the two arguments.
System.out.println("Math.min(5.7F,1.7F) = "+ Math.min(5.7F,1.7F));
//Math.min(5.7F,1.7F) = 1.7

System.out.println("Math.min(6,9) = "+ Math.min(6,9));
//Math.min(6,9) = 6

//Returns the larger of the two arguments.
System.out.println("Math.max(5.7F,1.7F) = "+ Math.max(5.7F,1.7F));
//Math.max(5.7F,1.7F) = 5.7

System.out.println("Math.max(6,9) = "+ Math.max(6,9));
//Math.max(6,9) = 9
}
}


Output from The Program

Math.E = 2.718281828459045
Math.PI = 3.141592653589793
Math.abs(7.8) = 7.8
Math.abs(-9) = 9
Math.ceil(1.3) = 2.0
Math.ceil(-9.7) = -9.0
Math.floor(1.3) = 1.0
Math.floor(-9.7) = -10.0
Math.rint(1.3) = 1.0
Math.rint(-9.7) = -10.0
Math.rint(2.5) = 2.0
Math.round(2.587) = 3
Math.round(-9.132) = -9
Math.round(5.7F) = 6
Math.min(5.7F,1.7F) = 1.7
Math.min(6,9) = 6
Math.max(5.7F,1.7F) = 5.7
Math.max(6,9) = 9

Java Tutorial Beyond Basic Arithmetic

張貼留言

0 留言