0% found this document useful (0 votes)
65 views

Math Methods-Cube Root

The document discusses various Math methods in Java including Math.cbrt(), Math.log(), and Math.abs(). It provides the syntax and examples of using each method to find the cube root, natural logarithm, and absolute value respectively. For each method, it also describes the return values for different types of inputs such as positive/negative numbers, NaN, infinity, and zero values.

Uploaded by

gayathri naidu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views

Math Methods-Cube Root

The document discusses various Math methods in Java including Math.cbrt(), Math.log(), and Math.abs(). It provides the syntax and examples of using each method to find the cube root, natural logarithm, and absolute value respectively. For each method, it also describes the return values for different types of inputs such as positive/negative numbers, NaN, infinity, and zero values.

Uploaded by

gayathri naidu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

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
 
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.abs()

If we provide positive or negative value as argument, this


method will result positive value.
If the argument is Infinity, this method will result Positive
Infinity.
If the argument is NaN, this method will return NaN.
Math Methods
Example

public class AbsExample1  
{  
    public static void main(String args[])  
    {  
        int x = 78;  
        int y = -48;    
   System.out.println(Math.abs(x));  
   System.out.println(Math.abs(y));  
   System.out.println(Math.abs(Integer.MIN_VALUE));  
    }  
}  
Math Methods
Math Methods
Example

public class AbsExample2   
{   
    public static void main(String args[])  
    {  
        double x = -47.63;  
        double y = -894.37;   
        System.out.println(Math.abs(x));  
        System.out.println(Math.abs(y));  
        System.out.println(Math.abs(7.0 / 0));     
    }  

Math Methods
Math Methods
Example

public class AbsExample3  
{  
    public static void main(String args[])  
    {  
        float x = -73.02f;  
        float y = -428.0f;    
        //print the absolute value of float type  
        System.out.println(Math.abs(x));  
        System.out.println(Math.abs(y));  
    }  
}  
Math Methods
 
Math Methods
Example

public class AbsExample4   
{  
    public static void main(String args[])  
    {  
        long x = 78730343;  
        long y = -4839233;   
        System.out.println(Math.abs(x));  
        System.out.println(Math.abs(y));  
        System.out.println(Math.abs(Long.MIN_VALUE));  
    }  
}  
Math Methods
 

You might also like