This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Java Math - Floor/Ceil returning double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Math - Floor/Ceil returning double" Watch "Java Math - Floor/Ceil returning double" New topic
Author

Java Math - Floor/Ceil returning double

Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Is there a good reason Math.floor() and Math.ceil() return double's instead of int (or more precisely long)? Is this just a matter of "it was done in JDK 1.0 this way and we can't change it" or is there some justification to it?

I haven't seen a better explanation other than "it's what the API documentation says", which doesn't really answer the question.


My Blog: Down Home Country Coding with Scott Selikoff
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It's certainly possible that the value returned by those methods might be outside of the range of int, or even long.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Well, if it was outside the range, wouldn't floor/ceil have no effect since the values past the decimal point were already truncated?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

What decimal point? We're talking about doubles here, not BigDecimal.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

I think what Scott's trying to say is that Long.MAX_VALUE is 9223372036854775807 and (double) Long.MAX_VALUE is 9223372036854776000. Such large double precision numbers can't have a fractional part, so floor(...) or ceiling(...) does nothing.


luck, db
There are no new questions, but there may be new answers.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I know what Scott is trying to say. But you missed what I was trying to say -- I can tell that because you used the phrase "fractional part". That's the decimal assumption again. Double values are binary, not decimal. I predict that you will find that

doesn't produce an integer.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Besides being outside the range of significant digits, there's also a question of practical uses. In the vast majority of circumstances where I want to use floor/ceil, my numbers are well within long, even int ranges. It's hard for me to imagine a use where someone would want the floor/top of a number greater than the long limit, and even if they do these functions have no effect.

BTW, even if 10.0E100 / 7.0 is not an integer, the decimal value will be truncated in double since mantissa cannot express it, as demonstrated in the following code which outputs "true,true":



Note that if 10.0E10 is used as x, the output is "false,false", but then y and z are fully expressible within the limit of long.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

I was looking through the Java source, and apparently values too large are ignored:



In short, the argument that ceil/floor return double 'because the input may be outside the range of long' is flawed because in actuality you should never call ceil/floor on those values anyway.

Also, there's a consistency argument to be made here, since Math.round() does return long/int. I still think they made a mistake with ceil/floor when they had it return double and I have yet to be persuaded otherwise.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That's true... there will be some point beyond which only integer values can be expressed by a double value. Presumably that's why the code you posted exists. So is that point higher than the maximum value for a long?
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Well, I'm asking if there is a valid reason why it doesn't return an integer value and it doesn't seem like there is. It's a choice between making developers checking the size of their exponent (if they expect a value higher than the long range) versus making developers cast the output to int/long. Since the latter seems far more common to me in every day programming, I think the original Java developers made a mistake doing it this way. It should return a long value and throw an exception if over the long range, since it should not have been called on such a value in the first place.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You seem to think it's a virtue that Math.round(double) returns a long. Given this implementation detail:
The API docs wrote:If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.

I don't agree with that.

But you're basically saying "Well, in real life people aren't going to be using double values outside of the range of long anyway so it doesn't matter"... or am I straw-manning you?
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Paul Clapham wrote:But you're basically saying "Well, in real life people aren't going to be using double values outside of the range of long anyway so it doesn't matter"... or am I straw-manning you?


No, I'm saying in real life you *should* never call ceil/floor on a value outside the long range since it has no effect. If that rule was followed, or alternatively if an exception was thrown if they did use such a large value, then the ceil/floor() could return long without issue.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Scott Selikoff wrote:No, I'm saying in real life you *should* never call ceil/floor on a value outside the long range since it has no effect.

So are you saying that I shouldn't be allowed to call ceil/floor on a value outside the long range? Because in real life I'm not going to surround all my calls to Math.floor by an if-statement which checks that I'm not going to do something which isn't allowed, even though that something is perfectly well-defined.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

To me, calling ceil/floor on a double above the long range amounts to a programmer error. It's conceivable the call to check the number is outside the range is less expensive then the call to perform the truncation (since it includes the check implicitly), therefore its a wasted call.

I'm not saying everything should be surrounded by an if-statement, but if you are in a position where you need ceil/floor of a number, then's a good bet you know you are within long range already. BTW, I consider it a waste that developers have to cast return values from ceil/floor... I imagine the quantity of those casts far exceeds the number of times someone would need to implement the if statement.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I disagree. When you write code, you don't necessarily know what data people are going to put into that code. This isn't like having to check for null so you avoid NullPointerException, this would be like having to check for excessively big numbers. Just because you're only envisioning applications which use small numbers doesn't mean you should be forcing people with applications which do use large numbers to do workarounds so they can actually use them.

Likewise I disagree about "wasted calls"; the code you posted shows that isn't the case. And having to cast the double to long isn't a waste either; if the method had been declared to return a long, the cast would still be taking place, it would just be hidden inside the system code.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

I guess we'll have to agree to disagree. I don't see the value in calling a method that returns the exact same value, of which the call could be avoided altogether. Having these functions return int/long would be a lot more useful as a developer. In short, both strategies do work, neither offers something the other does not. The difference is what kind of workload you want to put on the developer.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I agree that we'll have to disagree. For me the design error was having the round() method truncate the returned value down into the long range. We do agree that round() and floor() shouldn't be inconsistent in that way, we just disagree on how to fix it (in that alternate universe).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Math - Floor/Ceil returning double