Professional Documents
Culture Documents
Mathematical Library Methods
Mathematical Library Methods
Introduction
import java.lang.*;
public class PowExample1
{
public static void main(String[] args)
{
double x = 5;
double y = 4;
//returns 5 power of 4 i.e. 5*5*5*5
System.out.println(Math.pow(x, y));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class PowExample2
{
public static void main(String[] args)
{
double x = 9.0;
double y = -3;
//return (9) power of -3
System.out.println(Math.pow(x, y));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class PowExample3
{
public static void main(String[] args)
{
double x = -765;
double y = 0.7;
//return NaN
System.out.println(Math.pow(x, y));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class PowExample4
{
public static void main(String[] args)
{
double x = 27.2;
double y = 1.0;
// Second argument is 1 so output is 27.2
System.out.println(Math.pow(x, y));
}
}
Math Methods
Math Methods
Math.sqrt()
It is used to find the square root of a positive number. It
returns a double type value.
If the argument is NaN or less than zero, this method will
return NaN.
If the argument is positive infinity, this method will return
positive Infinity.
If the argument is positive or negative Zero, this method
will return the result as Zero with same sign.
Math Methods
Example
import java.lang.*;
public class SqrtExample1
{
public static void main(String[] args)
{
double x = 81.0;
// Input positive value, Output square root of x
System.out.println(Math.sqrt(x));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class SqrtExample2
{
public static void main(String[] args)
{
double x = -81.78;
// Input negative value, Output NaN
System.out.println(Math.sqrt(x));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class SqrtExample3
{
public static void main(String[] args)
{
double x = 0.0/0;
// Input NaN, Output NaN
System.out.println(Math.sqrt(x));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class SqrtExample4
{
public static void main(String[] args)
{
double x = 1.0/0;
// Input positive infinity, Output positive infinity
System.out.println(Math.sqrt(x));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class SqrtExample5
{
public static void main(String[] args)
{
double x = 0.0;
// Input positive Zero, Output positive zero
System.out.println(Math.cbrt(x));
}
}
Math Methods
Math Methods
Math.cbrt()
It is used to find the cube root of a positive or a negative
number. It always returns a value in double data type.
import java.lang.*;
public class CbrtExample1
{
public static void main(String[] args)
{
double x = 729;
//return the cube root of x
System.out.println(Math.cbrt(x));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class CbrtExample2
{
public static void main(String[] args)
{
double x = 1.0/0;
// Input positive infinity, Output positive infinity
System.out.println(Math.cbrt(x));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class CbrtExample3
{
public static void main(String[] args)
{
double x = -0.0;
// Input negative Zero, Output negative zero
System.out.println(Math.cbrt(x));
}
}
Math Methods
Math Methods
Math.log()
It is used to find the natural logarithmic value of a given
argument. It always returns a double type value.
public class LogExample1
{
public static void main(String[] args)
{
double x = 38.9;
// Input positive double, output logarithm of x
System.out.println(Math.log(x));
}
}
Math Methods
Math Methods
Math Methods
Example
public class LogExample2
{
public static void main(String[] args)
{
double x = 189.45;
// Input positive double, output logarithm of x
System.out.println(Math.log(x));
}
}
Math Methods
Math Methods
Example
public class LogExample2
{
public static void main(String[] args)
{
double x = -70.4;
// Input negative double, output NaN
System.out.println(Math.log(x));
}
}
Math Methods
Math Methods
Example
public class LogExample3
{
public static void main(String[] args)
{
double x = 1.0/0;
// Input positive infinity, output Infinity
System.out.println(Math.log(x));
}
}
Math Methods
Math Methods
Example
public class LogExample4
{
public static void main(String[] args)
{
double x = 0;
// Input positive zero, output -Infinity
System.out.println(Math.log(x));
}
}
Math Methods
Math Methods
Math.abs()
It always return the absolute value of an argument. The
return value may be int/long/double data type depending
upon the input arguments.
E value is 2.718281828
Math Methods
Math.random()
It returns a random number between 0 and 1 in a double data
type value.
Degree = 22/7*Radian*1/180
Radian = 22/(7*180)*Degree