Let’s discuss more
about the Math functions in Java and if you want to know what is math
functions, you can CHECK HERE.
Math.floor(x)
Whatever be the
number after the decimal point value, those numbers are neglected while using
floor math function.
Eg) If x=11.78 and
when floor math function is implemented on it, the output becomes 11.
CODE
class java {
public static void main(String args[]) {
//Declare float variable
float a=32.0f, b=32.78f;
System.out.println("The floor output of a is ");
System.out.println(Math.floor(a));
System.out.println("The floor output of b is ");
System.out.println(Math.floor(b));
}
}
Math.floorDiv(x,y)
Here, two input
variables are taken and division operation is performed. Finally, the floor
value of the result is given in the output.
Eg) Let x and y be
11 and 2 respectively. First 11/2 operation is done which results in 5.5 and
then it is rounded off to 5 since it’s the floor value.
CODE
class java {
public static void main(String args[]) {
//Declare float variable
int a = 47, b = 5;
System.out.println("The floorDiv output is ");
System.out.println(Math.floorDiv(a, b));
}
}
Output
Math.random(x)
As the name
suggests, the code prints a random double value from0 to less than 1 as its
output. The beauty of this Math function is every time the code is executed, it
produces different outputs. This random math function can be used only for
double data type.
CODE
class java {
public static void main(String args[]) {
//Assigning the random number to double
double a=Math.random();
System.out.println("The random output is ");
System.out.println(a);
}
}
Output
Math.hypot(x,y)
This value returns
out the hypotenuse value of x and y. This function is based on 3 cases:
Case (i) Both
integers
When both the
integers are positive, the output is sqrt ((x*x) + (y*y))
Case (ii) Any one
value is infinity
When either or
both of the input is infinity, the output is also infinite.
Eg) 1/0 is
infinity
Case (iii) Not a
Number (NaN case)
When any one of
the inputs is in NaN condition, the output is also NaN. Eg) 0/0 becomes NaN
CODE
class java {
public static void main(String args[]) {
//Declaring variables
double a=4, b=3, c=-4, d=-3;
// Case 1
//Hypotenuse of positive numbers
System.out.println("The hypotenuse output is ");
System.out.println(Math.hypot(a,b));
//hypotenuse of negative numbers
System.out.println("The hypotenuse output is ");
System.out.println(Math.hypot(c,d));
//Case 2: Infinity case
//Declaring an infinity number
double e=Double.POSITIVE_INFINITY;
System.out.println("Hypotenuse output is");
System.out.println(Math.hypot(e,a));
//Case 3:Nan
//Declaring NaN variable
double f=0.0/0;
System.out.println("Hypotenuse output is");
System.out.println(Math.hypot(f,b));
}
}
Output
Math.addExact(x, y),
This math function
is used to add two numbers instead of using an addition operator. Similarly,
Math.subtractExact(x,y) and Math.multiplyExact(x,y) is used to subtract and
multiply the inputs respectively.
CODE
class java {
public static void main(String args[]) {
int a = 11;
int b = 29;
System.out.println(Math.addExact(a, b));
System.out.println(Math.subtractExact(a, b));
System.out.println(Math.multiplyExact(a, b));
}
}
Output
Math.incrementExact(x),
Math.decrementExact(x)
Using this Math
function, the value is incremented by 1. Similarly, Math.decrementExact(x) is
used to decrement the number by 1.
class java {
public static void main(String args[]) {
int a = 11;
int b = -29;
System.out.println(Math.incrementExact(a));
System.out.println(Math.incrementExact(b));
System.out.println(Math.decrementExact(a));
System.out.println(Math.decrementExact(b));
}
}
Output
Math.negateExact(x)
This Math function
is used to determine the negative value of the input. For instance, if x=11,
the output is -11 and when the input x=-29, the output is 29.
class java {
public static void main(String args[]) {
int a = 11;
int b = -29;
System.out.println(Math.negateExact(a));
System.out.println(Math.negateExact(b));
}
}
Output









0 Comments