public class MathLibraryinJava
{
public static void main(String[] args)
{
double rint;
double x = 62.5;
double y = 69.7;
int i, j;
double a, b, c, num;
a = 9.2;
b = 9.9;
c = -5;
System.out.println("Math Function in Java");
System.out.println("=====================");
// Math.abs() will return absolute value(without any sign)
System.out.println(c + " Absolute value is " + Math.abs(c));
/*
Math.round() method returns the value in rounded form.
It will return the same value if the number is
below ½ or .5 otherwise it return the next higher integer value.
*/
System.out.println(x + " is approximately " + Math.round(x));
System.out.println(y + " is approximately " + Math.round(y));
/*The Math.ceil() return the rounded value to the
* next higher integer and it return double type value */
System.out.println("The ceiling of " + a + " is " + Math.ceil(a));
System.out.println("The ceiling of " + b + " is " + Math.ceil(b));
System.out.println("The ceiling of " + x + " is " + Math.ceil(x));
System.out.println("The ceiling of " + y + " is " + Math.ceil(y));
// Math.floor() returns a number down to the nearest integer
// it can round off a floating point number
System.out.println("The floor of " + a + " is " + Math.floor(a));
System.out.println("The floor of " + b + " is " + Math.floor(b));
System.out.println("The floor of " + x + " is " + Math.floor(x));
System.out.println("The floor of " + y + " is " + Math.floor(y));
// Math.rint() will returns the truncated value of the given number
// that is, the integer part of the value by removing fractional part
rint = 8.5;
System.out.println("The rint of " + rint + " is " + Math.rint(rint));
rint = 8.84;
System.out.println("The rint of " + rint + " is " + Math.rint(rint));
// Comparison operators
// min() returns the smaller of the two numbers.
// The value return's depending upon the number used as the
// arguments to the method
System.out.println("min(" + a + "," + b + ") is " + Math.min(a,b));
System.out.println("min(" + x + "," + y + ") is " + Math.min(x,y));
System.out.println("min(" + a + "," + b + ") is " + Math.min(a,b));
System.out.println("min(" + y + "," + b + ") is " + Math.min(y,b));
// There's a corresponding max() method
// that returns the larger of two numbers
// The value return's depending upon the number used as the
// arguments to the method
System.out.println("max(" + a + "," + b + ") is " + Math.max(a,b));
System.out.println("max(" + x + "," + y + ") is " + Math.max(x,y));
System.out.println("max(" + a + "," + x + ") is " + Math.max(a,x));
System.out.println("max(" + a + "," + b + ") is " + Math.max(a,b));
// The Math library Math.PI and Math.E defines
// constant value
System.out.println("Pi value is " + Math.PI);
System.out.println("e value is " + Math.E);
// The trigonometric methods in Java
// parameter or arguments / parameters are given in radians
// radiant = (22/(7*180))*Degree
// Convert a 30 degree angle to radians
double angle = 30.0 * 2.0 * Math.PI/360.0;
System.out.println("cos(" + angle + ") is " + Math.cos(angle));
System.out.println("sin(" + angle + ") is " + Math.sin(angle));
// Inverse Trigonometric methods
// All values are returned as radians
double radians = 1.5d;
double sine = Math.sin(radians);
double cosine = Math.cos(radians);
double tan = Math.tan(radians);
double asine = Math.asin(sine);
double acosine = Math.acos(cosine);
double atan = Math.atan(tan);
System.out.println("The value Sine of " + radians + " = " + sine);
System.out.println("The value Cosine of " + radians + " = " + cosine);
System.out.println("The value Tangent of " + radians + " = " + tan);
System.out.println("The value Arcsine of " + sine + " = " + asine);
System.out.println("The value Arccosine of " + cosine + " = " + acosine);
System.out.println("The value Arctangent of " + tan + " = " + atan);
// Exponential and Logarithmic Methods
// Math.exp(2.0) returns e (7.38905609893065) raised
// to the power of a.
System.out.println("exp(3.0) is " + Math.exp(3.0));
System.out.println("exp(20.0) is " + Math.exp(20.0));
System.out.println("exp(0.0) is " + Math.exp(0.0));
// Math.log(a) returns the natural
// logarithm (base e) of a.
System.out.println("log(2.0) is " + Math.log(1.0));
System.out.println("log(10.0) is " + Math.log(10.0));
System.out.println("log(Math.E) is " + Math.log(Math.E));
// Math.pow(x, y) returns the x raised
// to the yth power.
System.out.println("pow(10, 3) is " + Math.pow(10,3));
System.out.println("pow(5.0, 4.5) is " + Math.pow(5.0,4.5));
System.out.println("pow(10, -1) is " + Math.pow(10,-1));
// Math.sqrt(num) returns the square root of num
num = 25;
System.out.println("The square root of " + num + " is " + Math.sqrt(num));
// Math.cbrt() return the cube root of number
num = 125;
System.out.println("The cube root of " + num + " is " + Math.cbrt(num));
// Random method Math.random() will retrun
// pseudo-random number between 0.0 and 1.0;
System.out.println("Random number genetated : " + Math.random());
System.out.println("Here's 5 random number is: " + Math.random()*5);
}
}