# Difference Math.rint() Math.round()

###### Difference Math.rint() Math.round()

First find the preliminary difference of rint() and round() methods

1. System.out.println(Math.rint(2.50)); // prints 2.0
2. System.out.println(Math.round(2.50)); // prints 3

Let us study more:

1. With rint()

a) 2.50 lies between 2.00 and 3.00. rint() returns the closest even double value. The rint(2.50) returns 2.0
b) 1.50 lies between 1.00 and 2.00. rint() returns the closest even double value. The rint(1.50) returns 2.0

2. With round()

a) 2.50 lies between 2.00 and 3.00. round() returns the closest higher whole number. The round(2.50) returns 3
b) 1.50 lies between 1.00 and 2.00. round() returns the closest higher whole number. The round(1.50) returns 2

Note: rint() returns a double value and round() returns a whole number of int/long.

3. If the argument is already an integer value, then both methods return the same value of that of the argument.

1. System.out.println(Math.rint(3.0)); // prints 3.0
2. System.out.println(Math.round(3.0)); // prints 3

4. If by chance, the argument is positive or negative infinity or NaN, the result will slightly differ.

1. System.out.println(Math.sqrt(-25.0)); // prints NaN
2. System.out.println(Math.rint(Math.sqrt(-25.0))); // prints NaN, rint returns as it is of argument
3. System.out.println(Math.round(Math.sqrt(-25.0))); // prints 0; if the argument is NaN, round returns 0 value

5. round() Evaluation

round(double a) returns a value which is equivalent to (int)Math.floor(a + 0.5f). That is, add 0.5 to the argument and round it as usual. You land in a correct answer.

6. With rint() and round()

a) If the argument is NaN, the result is 0.
b) If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.
c) If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.

1. System.out.println(Math.rint(2.5/0)); // prints Infinity
2. System.out.println(Math.rint(-2.5/0)); // prints -Infinity

3. System.out.println(Math.round(2.5/0)); // prints 9223372036854775807. Returns long data type range
4. System.out.println(Math.round(-2.5/0)); // prints -9223372036854775808. Returns long data type range