-4

Today, I saw this behaviour of Java and Javascript's Math.round function.

It makes 1.40 to 1 as well as -1.40 to -1

It makes 1.60 to 2 as well as -1.60 to -2

Now, it makes 1.5 to 2. But, makes -1.5 to -1.

I checked this behaviour in round equivalents of PhP and MySQL as well. Both gave results as expected. i.e. round(-1.5) to -2

Even the Math.round definition says it should round it to nearest integer.

Wanted to know why is it so?

Sujit Singh
  • 921
  • 1
  • 10
  • 20
  • 13
    nearest integer, means the nearest upper integer. `-1` is greater than `-1.5`. – Tunaki Apr 12 '16 at 11:14
  • @Tunki: Shouldn't there be a mention for same? They just said "nearest integer" not "nearest upper integer" – Sujit Singh Apr 12 '16 at 11:27
  • You can find this in [`RoundingMode.HALF_EVEN`](https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html#HALF_EVEN) docs: *This rounding mode is analogous to the rounding policy used for float and double arithmetic in Java.*. Otherwise, refer to [4.2.3](https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.3) of the Java spec. – Tunaki Apr 12 '16 at 11:37

3 Answers3

2

The problem is that the distance between 1 and 1.5 as well as 1.5 and 2 is exactly the same (0.5). There are several different ways you now could round:

  • always towards positive infinity
  • always towards negative infinity
  • always towards zero
  • always away from zero
  • towards nearest odd or even number
  • ... (see Wikipedia)

Obviously, both Java and JS opted for the first one (which is not uncommon) while PHP and MySql round away from zero.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
1

Rounding mode to round towards negative infinity. If the result is positive, behave as for RoundingMode.DOWN; if negative, behave as for RoundingMode.UP. Note that this rounding mode never increases the calculated value. It is just matter of whole number and its position against number chart. From here you can see javadocs.

public 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.

    Parameters:

a - a floating-point value to be rounded to an integer.

Returns:

the value of the argument rounded to the nearest int value. Review this link too

manikant gautam
  • 3,521
  • 1
  • 17
  • 27
  • It is better to include the relevant quote from the javadoc, than just a link. – Peter Lawrey Apr 12 '16 at 11:17
  • I think you should link to [round](https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#round(double)), too. Not just RoundingMode. – Nier Apr 12 '16 at 11:29
1

From the Ecma script documentation,

Returns the Number value that is closest to x and is equal to a mathematical integer. If two integer Number values are equally close to x, then the result is the Number value that is closer to +∞. If x is already an integer, the result is x.

where x is the number passed to Math.round().

So Math.round(1.5) will return 2 hence 2 is closer to +∞ while comparing with 1. Similarly Math.round(-1.5) will return -1 hence -1 is closer to +∞ while comparing with -2.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130