Mathematical Functions and Constants in Java Programming
Previous | Home | Next |
The Math class in the java.lang package provides methods and constants for doing more advanced mathematical computation.
The Java programming language supports basic arithmetics with its arithmetic operators: +, -, *, /, and %.
The Math class in the java.lang package provides methods and constants for doing more advanced mathematical computation.
Math consists two constants
Math.E, which is the base of natural logarithms, and
Math.PI, which is the ratio of the circumference of a circle to its diameter.
Math class in java support almost forty static methods few are basic ones which are listed below with their description.
double abs(double d)
float abs(float f)
int abs(int i) All these are Returns the absolute value of the argument.
long abs(long lng)
double exp(double d) Returns the base of the natural logarithms, e, to the power of the argument.
double ceil(double d)
double floor(double d) Returns the largest integer that is less than or equal to the argument. Returned as a double.
public class MathDemo {
public static void main(String[] args) {
double a =151.635;
double b =78.74;
int c = 16, d = 45;
System.out.printf("The absolute value of %.3f is %.3f%n", a, Math.abs(a));
System.out.printf("The ceiling of %.2f is %.0f%n", b, Math.ceil(b));
System.out.printf("The floor of %.2f is %.0f%n", b, Math.floor(b));
System.out.printf("The rint of %.2f is %.0f%n", b, Math.rint(b));
System.out.printf("The max of %d and %d is %d%n",c, d, Math.max(c, d));
System.out.printf("The min of of %d and %d is %d%n",c, d, Math.min(c, d));
Previous | Home | Next |