Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 68

Mathematical Library Methods

Introduction

The library methods are built-in methods designed


by the developers. These predefined functions help the user
to perform certain tasks easily and quickly which are
frequently used in java programming.

General Syntax : Math.<method name>


Math Methods
Math.min()
It returns the minimum of two numbers. It returns
int/long/double type value depending upon the type of
arguments passed to it.

General Syntax : <Return data type><variable> =


<Function name(argument 1, argument 2)>;

Example : double m=Math.min(-4.5, -5.4);


Output : -5.4
Math Methods
Example
import java.lang.*;
public class MinExample1
{
public static void main(String args[])
{
int x = 20;
int y = 50;
//print the minimum of two numbers
System.out.println(Math.min(x, y));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class MinExample2
{
public static void main(String args[])
{
double x = 25.67;
double y = -38.67;
//print the minimum of two numbers
System.out.println(Math.min(x, y));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class MinExample3
{
public static void main(String args[])
{
float x = -55.73f;
float y = -30.95f;
float z = -75.45f;
//print the minimum of three numbers
System.out.println(Math.min(Math.min(x, y), z));
}
}
Math Methods
Math Methods
Math.max()
It returns the greatest value of two numbers. It returns
int/long/double type value depending upon the type of
arguments passed to it.

General Syntax : <Return data type><variable> =


<Function name(argument 1, argument 2)>;

Example : double h=Math.max(9.2, 9.45);


Output : 9.45
Math Methods
Example
import java.lang.*;
public class MaxExample1
{
public static void main(String args[])
{
int x = 33;
int y = 75;
//print the maximum of two numbers
System.out.println(Math.max(x, y));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class MaxExample2
{
public static void main(String args[])
{
double x = 12.123;
double y = 12.456;
//print the maximum of two numbers
System.out.println(Math.max(x, y));
}
}
Math Methods
Math Methods
Example
import java.lang.*;
public class MaxExample3
{
public static void main(String args[])
{
float x = -85.63f;
float y = -23.45f;
float z = -79.12f;
//print the maximum of three numbers
System.out.println(Math.max(Math.max(x, y), z));
}
}
Math Methods
Math Methods
Math.pow()
This method is used to find the power raised to a given base
value. It always returns a double type value.

General Syntax : <Return data type><variable> =


<Function name(argument 1, argument 2)>;

Example : double p=Math.pow(3.0, 4.0);


Output : 81.0
Math Methods
Math.pow()

If the second argument is positive or negative zero, this


method will return 1.0.
If the second argument is not a number (NaN), this method
will return NaN.
If the second argument is 1, this method will return the
result same as the first argument.
Math Methods
Example

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.

General Syntax : <Return data type><variable> =


<Function name(Positive Number)>;

Example : double s=Math.sqrt(6.25);


Output : 2.5
Math Methods
Math.sqrt()

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.

General Syntax : <Return data type><variable> =


<Function name(Positive Number)>;

Example : double c=Math.cbrt(35.937);


Output : 3.3
Math Methods
Math.cbrt()

If the argument is NaN, this method will return NaN.


If the argument is infinity, this method will
return Infinity with same sign as the argument.
If the argument is positive or negative Zero, this method
will return Zero with same sign as the argument.
Math Methods
Example

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.

General Syntax : <Return data type><variable> =


<Function name(Positive Number)>;

Example : double l=Math.log(6.25);


Output : 1.8325
Math Methods
Math.log()

If the argument is Positive value, this method will return


the logarithm of a given value.
If the argument is Negative value, this method will
return NaN.
If the argument is not a number (NaN), 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 Negative Infinity.
Math Methods
Example

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.

General Syntax : <Return data type><variable> =


<Function name(Number)>;

Example : double a=Math.abs(-12.45);


Output : 12.45
Math Methods
Math.round()
This method returns the value of a number rounded to its
nearest integer. If the fractional part is less than 0.5 then it
returns the same integer value, otherwise it returns the next
higher integer. It gives different output for positive and
negative numbers and it returns the value in long/int.

General Syntax : <Return data type><variable> =


<Function name(Argument)>;
Example : double r=Math.round(-8.5);
Output : 8
Example : double r=Math.round(4.5);
Output : 5
Math Methods
Math.rint()
This function returns the nearest integer of a given fractional
number. The return value data type will always be double.

General Syntax : <Return data type><variable> =


<Function name(Argument)>;

Example : double i=Math.rint(9.5);


Output : 9.0
Example : double i=Math.rint(-6.5);
Output : -7.0
Math Methods
Math.ceil()
It returns the next higher integer number that is greater than
or equal to the argument. It always returns the value as a
double data type.

General Syntax : <Return data type><variable> =


<Function name(Argument)>;

Example : double c=Math.ceil(3.5);


Output : 4.0
Example : double c=Math.ceil(-3.5);
Output : -3.0
Math Methods
Math.floor()
It returns the lower integer number that is less than or equal
to the argument. It always returns the value as a double data
type.

General Syntax : <Return data type><variable> =


<Function name(Argument)>;

Example : double f=Math.floor(6.912);


Output : 6.0
Example : double f=Math.floor(-6.912);
Output : -7.0
Math Methods
Math.exp()
It returns in the exponential value of an argument x (i.e. ex).
It returns a double type value.

General Syntax : <Return data type><variable> =


<Function name(Number)>;

Example : double e=Math.exp(6.25)


Output : 518.0128

E value is 2.718281828
Math Methods
Math.random()
It returns a random number between 0 and 1 in a double data
type value.

General Syntax : <Return data type><variable> =


<Function name()>;

Example : double r=Math.random()


Output : 0.6180780691
Math Methods
Trigonometrical Functions
There are some Trigonometrical functions which are
frequently used to find sine, cosine and tangent values of a
given angle(in radians). Angles are passed as an argument to
the function.

General Syntax : <Return data type><variable> =


<Function name(Value in Radian)>;

Example : double t=Math.sin(30o)


double t=Math.sin(0.00058201058)
Output : 0.50
Math Methods
Converting Degree to Radian

Formula : 180 Degree = 22/7*Radian

Degree = 22/7*Radian*1/180

Radian = 22/(7*180)*Degree

You might also like