Ad Code

Responsive Advertisement

Basic Math functions

 

Math class

The Math class is already an inbuilt feature of Java and this class roughly contains more than 100 functions and methods. Some interesting functions using Math class is discussed below:



Math.abs (x)

Let’s consider ‘x’ as an integer value. If you want to have the positive value of ‘x’ in the output irrespective of the input integer’s sign, you can use this abs function.

Math.max(x,y) and Math.min(x,y)

As the name suggests, these two functions return the maximum and minimum values respectively.

Math.round(x)

This function accepts only floating point values and it rounds off based on the number present after the decimal point.

Eg) 11.5 is rounded as 12 since the number after the decimal point is equal to 5. So, if the number next to the decimal point is equal to or greater than 5, then the input number is incremented by 1. In case the input is 11.2, the input is rounded off to 11.

Math.sqrt (x) and Math.cbrt (y)

These functions return back the square roots and cube roots value of the input number.

Let’s implement all these functions in a single code:

class java{
   
public static void main(String args[]){
       
//Declaring positive and negative integers
       
int x=121;
        int
y=-1331;
       
//abs- output is positive though the input is positive or negative
       
System.out.println("The absolute value of x is "+ Math.abs(x));
       
System.out.println("The absolute value of y is "+Math.abs(x));
       
//Finding greatest among x and y
       
System.out.println("The greatest number is "+ Math.max(x,y));
       
//Finding least number among x and y
       
System.out.println("The least number is "+ Math.min(x,y));
       
//Declaring decimal number
       
float z=11.291129f;
        float
a=11.5f;
       
//Rounding off the value
       
System.out.println("The rounded off value of z is "+ Math.round(z));
       
System.out.println("The rounded off value of a is "+ Math.round(a));
       
//Square root of the number x
       
System.out.println("The square root of the number x is " + Math.sqrt(x));
       
//Cube root of the number y
       
System.out.println("The cube root of the number y is " + Math.cbrt(y));
   
}
}

 

Output

Math.pow(x,y)

This function returns the x raised to the power of y result. This is also applicable for floating point values.

Math.signum(x)

This function is used to determine the sign of the input number. When the input is a positive value, it returns +1 and when the input is negative, the result is -1.

Math.ceil(x)

This function is somewhat similar to rounding off a float number. Even if the number after the decimal point is 1, the input is increased to the next number.

Eg) When ceil function is applied to the input 11.1, the output becomes 12.

Math.copySign(x,y)

This function gives out the absolute value of the first number along with the sign mentioned in the second number.

Eg) Consider 2 integers p=11 and q= -23

When you use the function, Math.copySign(p,q), your output will be -11 since the absolute value of 11 is 11. But since ‘q’ is a negative number, the negative sign will be taken and hence the output becomes -11.

Suppose you use this function in the reverse order like Math.copySign(q,p), your output will be 23. Since, the absolute value of -23 is 23. Then the sign of ‘p’ is positive. Hence the output is also positive. The syntax for Math.copySign(x, y) is Math.copySign(absolute value, sign).

The below Java code is written based on the above four math functions.

class java{
   
public static void main(String args[]){
       
//Declaring two integers
       
int x=2, y=-7;
       
System.out.println("x^y="+ Math.pow(x,y));
       
//Determine the sign of input values
       
System.out.println("The sign of x is " + Math.signum(x));
       
System.out.println("The sign of y is " + Math.signum(y));
       
//Declaring 2 float variables
        
float g=11.1f, h=11.0f;
       
//Using ceil math function
       
System.out.println("the ceil of g is " + Math.ceil(g));
       
System.out.println("the ceil of h is " + Math.ceil(h));
       
//Assigning positive and negative integers
       
int a=11, k=-23;
       
//copySign example
       
System.out.println("The copy Sign output of a and k is "+ Math.copySign(a,k) );
       
System.out.println("The copy Sign output of k and a is "+ Math.copySign(k,a) );
   
}
}

 

Output



PREVIOUS

                                                                                                                                         NEXT                                  

Post a Comment

0 Comments