Why does Math.round return a long but Math.floor return a double?

19,770

Solution 1

There is no inconsistency: the methods are simply designed to follow different specifications.

So by design round rounds to a long and rint rounds to a double. This has always been the case since JDK 1.0.

Other methods were added in JDK 1.2 (e.g. toRadians, toDegrees); others were added in 1.5 (e.g. log10, ulp, signum, etc), and yet some more were added in 1.6 (e.g. copySign, getExponent, nextUp, etc) (look for the Since: metadata in the documentation); but round and rint have always had each other the way they are now since the beginning.

Arguably, perhaps instead of long round and double rint, it'd be more "consistent" to name them double round and long rlong, but this is argumentative. That said, if you insist on categorically calling this an "inconsistency", then the reason may be as unsatisfying as "because it's inevitable".

Here's a quote from Effective Java 2nd Edition, Item 40: Design method signatures carefully:

When in doubt, look to the Java library APIs for guidance. While there are plenty of inconsistencies -- inevitable, given the size and scope of these libraries -- there are also fair amount of consensus.

Distantly related questions

Solution 2

floor would have been chosen to match the standard c routine in math.h (rint, mentioned in another answer, is also present in that library, and returns a double, as in java).

but round was not a standard function in c at that time (it's not mentioned in C89 - c identifiers and standards; c99 does define round and it returns a double, as you would expect). it's normal for language designers to "borrow" ideas, so maybe it comes from some other language? fortran 77 doesn't have a function of that name and i am not sure what else would have been used back then as a reference. perhaps vb - that does have Round but, unfortunately for this theory, it returns a double (php too). interestingly, perl deliberately avoids defining round.

[update: hmmm. looks like smalltalk returns integers. i don't know enough about smalltalk to know if that is correct and/or general, and the method is called rounded, but it might be the source. smalltalk did influence java in some ways (although more conceptually than in details).]

if it's not smalltalk, then we're left with the hypothesis that someone simply chose poorly (given the implicit conversions possible in java it seems to me that returning a double would have been more useful, since then it can be used both while converting types and when doing floating point calculations).

in other words: functions common to java and c tend to be consistent with the c library standard at the time; the rest seem to be arbitrary, but this particular wrinkle may have come from smalltalk.

Solution 3

I agree, that it is odd that Math.round(double) returns long. If large double values are cast to long (which is what Math.round implicitly does), Long.MAX_VALUE is returned. An alternative is using Math.rint() in order to avoid that. However, Math.rint() has a somewhat strange rounding behavior: ties are settled by rounding to the even integer, i.e. 4.5 is rounded down to 4.0 but 5.5 is rounded up to 6.0). Another alternative is to use Math.floor(x+0.5). But be aware that 1.5 is rounded to 2 while -1.5 is rounded to -1, not -2. Yet another alternative is to use Math.round, but only if the number is in the range between Long.MIN_VALUE and Long.MAX_VALUE. Double precision floating point values outside this range are integers anyhow.

Unfortunately, why Math.round() returns long is unknown. Somebody made that decision, and he probably never gave an interview to tell us why. My guess is, that Math.round was designed to provide a better way (i.e., with rounding) for converting doubles to longs.

Share:
19,770
Leo Jay
Author by

Leo Jay

Updated on June 11, 2022

Comments

  • Leo Jay
    Leo Jay about 2 years

    Why the inconsistency?

  • NullUserException
    NullUserException almost 14 years
    -1 This argument is invalid. If they specified that floor() should return a double while ceil() should return an int, would that be "consistent"?
  • polygenelubricants
    polygenelubricants almost 14 years
    @NullUserException: floor and ceil are closely related. There's round and there's rint. I don't know what else do you want. I'm trying to answer this objectively, further speculation seems hypothetical and/or subjective.
  • NullUserException
    NullUserException almost 14 years
    Fair enough ... Still looks like this .rint() method was added later to address the inconsistency.
  • polygenelubricants
    polygenelubricants almost 14 years
    @NullUserException: According to the @since metadata, both rint and round were always there since the beginning. Some methods were added in 1.2, 1.5 and 1.6, and they're clearly marked as such.
  • Leo Jay
    Leo Jay almost 14 years
    I had never heard of Math.rint(). Thanks for you explanation. And I think rint is a stupid method name.
  • Amir Pashazadeh
    Amir Pashazadeh over 12 years
    Agreed to you message, but I still believe it was a bad method signature design. These methods shall return long too.
  • supercat
    supercat about 9 years
    @LeoJay: Having Math.rint() and Math.rlong return int and long respectively would have made a lot more sense than having Math.round() return long when given a double, and int when given any other primitive type.