java.math class and its methods | Set 3

java.math class and its methods | Set 1
java.math class and its methods | Set 2
java.math class methods1

  • ceil() : java.math.ceil(double a) method returns the smallest possible value which is either greater or equal to the argument passed. The returned value is a mathematical integer.Special Case :
    • Result is same, if the returned value is already a mathematical integer.
    • Result is same, if the passed argument is NaN or infinte or zero.
    • Result is negative zero, if the passed argument is less than zero but greater than -1.0

    Syntax:

    public static double ceil(double arg)
    Parameters:
    arg - the argument value
    Returns:
    smallest possible value(mathematical integer)
    which is either greater or equal to the argument passed
    
  • atan() : java.math.atan() method returns returns the arc tangent of the method argument value. The returned angle is in the range -pi/2 through pi/2.
    arc tan is inverse tan of the argument passed.
    atan(arg) = tan inverse of arg
    Special Case :



    • Result is NaN, if the passed argument is NaN or its absolute value is > 1.
    • Result is zero, if argument is zero.

    Syntax:

    public static double atan(double a)
    Parameters:
    a - the argument whose arc tangent value we need.
        argument is taken as radian
    Returns:
    arc tan value of the argument.
    
  • copySign() : java.math.copySign() method returns first floating-point argument but having the sign of second argument.
    Syntax:

    public static double copySign(double m, double s)
                        or
    public static float copySign(float m, float s)
    Parameters:
    m - magnitude 
    s - sign 
    Returns:
    returns second argument with sign of first floating-point argument.
    

Java code explaining atan(), ceil(), copySign() method in Math class.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program explaining Math class methods
// atan(), ceil(), copySign()
import java.math.*;
public class NewClass
{
  
    public static void main(String[] args)
    {
  
        // Use of atan() method
        double Atani = Math.atan(0);
        System.out.println("atan value of Atani : "+Atani);
        double x = Math.PI/2;
  
        // Use of toRadian() method
        x = Math.toRadians(x);
        double Atanj = Math.atan(x);
        System.out.println("atan value of Atanj : "+Atanj);
        System.out.println("");
  
  
        // Use of ceil() method
        double val = 15.34 ,ceilval;
        ceilval = Math.ceil(val);
        System.out.println("ceil value of val : "+ceilval);
        System.out.println("");
  
        double dblMag = val;
        double dblSign1 = 3;
        double dblSign2 = -3;
  
  
        // Use of copySign() method
        double result1 = Math.copySign(dblMag,dblSign1);
        System.out.println("copySign1 : "+result1);
  
        double result2 = Math.copySign(dblMag,dblSign2);
        System.out.println("copySign2 : "+result2);
          
    }
}

chevron_right


Output:

atan value of Atani : 0.0
atan value of Atanj : 0.0274087022410345

ceil value of val : 16.0

copySign1 : 15.34
copySign2 : -15.34
  • cosh() : java.math.cosh() method returns the hyperbolic cosine of the argument passed.
    Special cases :

    • Result is NaN, if argument is NaN.
    • Result is 1.0, if the argument is zero.
    • Result is +ve infinity, if argument is infinite.

    Syntax:

    public static double cosh(double arg)
    Parameters:
    arg - The number whose hyperbolic cosine is to be returned.
    Returns:
    the hyperbolic cosine of the argument arg.
    
  • decrementExact() : java.math.decrementExact() method decrements the value of passed argument by one.
    Syntax:

    public static int decrementExact(int arg)
                    or
    public static long decrementExact(long arg)
    Parameters:
    arg - argument passed. 
    Returns:
    return argument decremented by one.
    Throws:
    Exception if the result overflows long or int datatype, according to the
    argumented data type.
    
  • exp() : java.math.exp(double arg) method returns the Euler’s number raised to the power of double argument.
    Important cases:

    • Result is NaN, if argument is NaN.
    • Result is +ve infinity, if the argument is +ve infifnity.
    • Result is +ve zero, if argument is -ve infinity.

    Syntax:

    public static double exp(double arg)
    Parameters:
    arg - argument passed. 
    Returns:
    Euler’s number raised to the power of passed argument
    

Java code explaining exp(), decrementExact(), cosh() method in Math class.


filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program explaining Math class methods
// exp(), decrementExact(), cosh()
import java.math.*;
public class NewClass
{
  
    public static void main(String[] args)
    {
  
        // Use of cosh() method
        double value = 2;
        double coshValue = Math.cosh(value);
        System.out.println("Hyperbolic Cosine of "  + coshValue);
        System.out.println("");
  
        // Use of decrementExact() method
        int result = Math.decrementExact(3051);
        System.out.println("Use of decrementExact() : " + result);
        System.out.println("");
  
  
        // Use of exp() method
        // declare the exponent to be used
        double exponent = 34;
        // raise e to exponent declared
        double expVal = Math.exp(exponent);
        System.out.println("Value of exp : "+ expVal);
  
    }
}

chevron_right


Output:

Using addExact() : 9

acos value of Asini : NaN
acos value of Asinj : 0.054858647341251204

cube root : 6.0
  • incrementExact() : java.math.incrementExact() method returns the argument by incrementing it’s value.
    Syntax:
    public static int incrementExact(int arg)
                   or
    public static long incrementExact(long arg)
    Parameters:
    arg - the argument
    Returns:
    incremented value of the argument
  • log10() : java.math.log10() method returns the base10 logarithmic value of the passed argument.
    Syntax:
    public static double log(double arg)
    Parameters:
    arg - argument passed. 
    Returns:
    base10 logarithmic value of the argument passed.
    
  • pow() : java.math.pow(double b, double e) method returns the value as be
    Syntax:
    public static double pow(double b,double e)
    Parameters:
    b : base
    e : exponent 
    Returns:
    value as baseexponent
    

JAVA code explaining incrementExact(), log10(), pow() method in Math class.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program explaining MATH class methods
// incrementExact(), log10(), pow()
import java.lang.*;
public class NewClass
{
  
    public static void main(String[] args)
    {
        // Use of incrementExact() method
        int f1 = 30, f2 = -56;
        f1 =Math.incrementExact(f1);
        System.out.println("Incremented value of f1 : "+f1);
  
        f2 =Math.incrementExact(f2);
        System.out.println("Incremented value of f2 : "+f2);
        System.out.println("");
         
  
        // Use of log10() method
        double value = 10;
        double logValue = Math.log10(value);
        System.out.println("Log10 value of 10 : "+logValue);
        System.out.println("");
  
        // Use of pow() method
        double b = 10, e = 2;
        double power = Math.pow(b,e);
        System.out.println("Use of pow() : "+power);
  
    }
}

chevron_right


Output:

Incremented value of f1 : 31
Incremented value of f2 : -55

Log10 value of 10 : 1.0

Use of pow() : 100.0

Note : There is no need to create a new object in order to call these methods as the above discussed Math class methods are static.

This article is contributed by Mohit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : nidhi_biet



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.