Why does Math.round return a long but Math.floor return a double?
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.
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.
There is no inconsistency: the methods are simply designed to follow different specifications.
long round(double a)
- Returns the closest
long
to the argument.
- Returns the closest
double floor(double a)
- Returns the largest (closest to positive infinity)
double
value that is less than or equal to the argument and is equal to a mathematical integer. - Compare with
double ceil(double a)
- Returns the largest (closest to positive infinity)
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
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
- Why does
int num = Integer.getInteger("123")
throwNullPointerException
? - Most awkward/misleading method in Java Base API ?
- Most Astonishing Violation of the Principle of Least Astonishment