views:

151

answers:

2

Why the inconsistency?

A: 

Duplicate: why-does-math-floor-return-a-double

Georgy Bolyuba
No, I'm not asking why it returns double instead of long, what I'm asking is why the inconsistency.
Leo Jay
From my point of view, round is "suppose" to result in integer value. I expect to use result of round as integer. Results of floor and ceil I expect to be used in some further float-point calculations. Hence, "If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument" is important in case of ceil and floor, but not in case of round. But that is just me.
Georgy Bolyuba
+1  A: 

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

polygenelubricants
-1 This argument is invalid. If they specified that `floor()` should return a double while `ceil()` should return an int, would that be "consistent"?
NullUserException
@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.
polygenelubricants
Fair enough ... Still looks like this `.rint()` method was added later to address the inconsistency.
NullUserException
@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.
polygenelubricants
I had never heard of Math.rint(). Thanks for you explanation. And I think rint is a stupid method name.
Leo Jay