abs Difference between Math.rint() and Math.round() in Java




math.round in java (6)

There is a type return difference:

Math.round() returns long or int
Math.rint() returns double

But the crucial difference is in the numerical treatment of values at 0.5.

Math.round() converts 9.5 <= x < 10.5 to 10
Math.rint() converts 9.5 <= x <= 10.5 to 10.0

Math.round() converts 10.5 <= x < 11.5 to 11
Math.rint() converts 10.5 < x < 11.5 to 11.0

Math.round() converts 11.5 <= x < 12.5 to 12
Math.rint() converts 11.5 <= x <= 12.5 to 12.0

(Note the inequalities!) So Math.round() always rounds up at the mid-point (0.5): documentation.
Instead, Math.rint() favours the closest even number at the mid-point: documentation.

For example, try running the following trivial example:

public class HelloWorld{
    public static void main(String []args){
        System.out.println("Math.round() of 9.5 is " + Math.round(9.5));
        System.out.println("Math.round() of 10.5 is " + Math.round(10.5));
        System.out.println("Math.round() of 11.5 is " + Math.round(11.5));
        System.out.println("Math.round() of 12.5 is " + Math.round(12.5));
        System.out.println("Math.rint() of 9.5 is " + Math.rint(9.5));
        System.out.println("Math.rint() of 10.5 is " + Math.rint(10.5));
        System.out.println("Math.rint() of 11.5 is " + Math.rint(11.5));
        System.out.println("Math.rint() of 12.5 is " + Math.rint(12.5));
    }
}

Note that the current top answer is wrong. I tried proposing an edit to his post but it was rejected. So as per the rejection comments, I'm putting my edits as a new answer.

What is the difference between Math.rint() and Math.round()?


Math.rint() and Math.round() have several differences, but the one which might impact the business logic of a Java application the most is the way they handle rounding of integers which are on a boundary (e.g. 4.5 is on the boundary of 4 and 5). Consider the following code snippet and output:

double val1 = 4.2;
double val2 = 4.5;
System.out.println("Math.rint(" + val1 + ")    = " + Math.rint(val1));
System.out.println("Math.round(" + val1 + ")   = " + Math.round(val1));
System.out.println("Math.rint(" + val2 + ")    = " + Math.rint(val2));
System.out.println("Math.round(" + val2 + ")   = " + Math.round(val2));
System.out.println("Math.rint(" + (val2 + 0.001d) + ")  = " + Math.rint(val2 + 0.001d));
System.out.println("Math.round(" + (val2 + 0.001d) + ") = " + Math.round(val2 + 0.001d));

Output:

Math.rint(4.2)    = 4.0
Math.round(4.2)   = 4
Math.rint(4.5)    = 4.0
Math.round(4.5)   = 5
Math.rint(4.501)  = 5.0
Math.round(4.501) = 5

As you can see, Math.rint(4.5) actually rounds down, while Math.round(4.5) rounds up, and this deserves to be pointed out. However, in all other cases, they both exhibit the same rounding rules which we would expect.

Here is a useful Code Ranch article which briefly compares Math.rint() and Math.round(): http://www.coderanch.com/t/239803/java-programmer-OCPJP/certification/Difference-rint-methods-Math-class


Example of Math.rint(): 2.50 lies between 2.00 and 3.00. Math.rint() returns the closest even double value. Math.rint(2.50) returns 2.0.

Example of Math.round(): 2.50 lies between 2.00 and 3.00. Math.round() returns the closest higher whole number. Math.round(2.50) returns 3



Difference is at .5.

Math.round() converts [10.5, 11.5[ to 11
Math.rint() converts ]10.5, 11.5[ to 11.0

Math.round() returns long or int.
Math.rint() returns double.

So it can be said that Math.round() favors mid-point(0.5) for higher value.


They behave differently when passed something ending in .5, signed 0, NaN, or an Infinity.

Math.round accepts both doubles and floats, and has varying return types for some reason (long and int respectively, which are the smallest type large enough to cover the entire range represented by the parameter).

Math.rint accepts doubles and returns doubles. It is less "destructive" than Math.round because it doesn't alter values under several conditions (see below).

The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. It minimizes rounding errors that result from consistently rounding a midpoint value in a single direction.

(From Jon Skeet's answer in C#. The behavior of Math.Round in C# is more similar to Java's Math.rint, as confusing as that may be.)


From the docs:

static double rint(double a)

Returns the double value that is closest in value to the argument and is equal to a mathematical integer.

Returns the double value that is closest in value to the argument and is equal to a mathematical integer. If two double values that are mathematical integers are equally close, the result is the integer value that is even.

Special cases:

  • If the argument value is already equal to a mathematical integer, then the result is the same as the argument.

  • If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.

...

static long round(double a)

...

static int round(float a)

Returns the closest int to the argument, with ties rounding up.

Special cases:

  • If the argument is NaN, the result is 0.
  • 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.
  • 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.

You can also change the behavior of Math.round.

public enum RoundingMode
extends Enum<RoundingMode>

Specifies a rounding behavior for numerical operations capable of discarding precision. Each rounding mode indicates how the least significant returned digit of a rounded result is to be calculated. If fewer digits are returned than the digits needed to represent the exact numerical result, the discarded digits will be referred to as the discarded fraction regardless the digits' contribution to the value of the number. In other words, considered as a numerical value, the discarded fraction could have an absolute value greater than one.

Each rounding mode description includes a table listing how different two-digit decimal values would round to a one digit decimal value under the rounding mode in question. The result column in the tables could be gotten by creating a BigDecimal number with the specified value, forming a MathContext object with the proper settings (precision set to 1, and the roundingMode set to the rounding mode in question), and calling round on this number with the proper MathContext. A summary table showing the results of these rounding operations for all rounding modes appears below.

      |  Result of rounding input to one digit with the given rounding 
______ı________________________________________________________________________________________
      |
Input |   UP    DOWN   CEILING    FLOOR    HALF_UP    HALF_DOWN    HALF_EVEN    UNNECESSARY
 5.5  |   6      5        6         5         6           5            6        throw ArithmeticException
 2.5  |   3      2        3         2         3           2            2        throw ArithmeticException
 1.6  |   2      1        2         1         2           2            2        throw ArithmeticException
 1.1  |   2      1        2         1         1           1            1        throw ArithmeticException
 1.0  |   1      1        1         1         1           1            1         1
-1.0  |  -1     -1       -1        -1        -1          -1           -1        -1
-1.1  |  -2     -1       -1        -2        -1          -1           -1        throw ArithmeticException
-1.6  |  -2     -1       -1        -2        -2          -2           -2        throw ArithmeticException
-2.5  |  -3     -2       -2        -3        -3          -2           -2        throw ArithmeticException
-5.5  |  -6     -5       -5        -6        -6          -5           -6        throw ArithmeticException




rounding