Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Math.floor return a double?

Tags:

java

types

math

People also ask

Why do Math ceilings have a double return?

The smallest whole number greater than or equal to a. If a is equal to NaN, NegativeInfinity, or PositiveInfinity, that value is returned. Therefore the return value has to be double since NaN, NegativeInfinity and PositiveInfinity are fields of Double.

Does Math floor return an int?

The Java Math floor() is a mathematical function available in Java Math Library. This function returns the closest integer value (represented as a double value) which is less than or equal to the given double value.

Why is Math round returning a long?

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.

What happens when you use the Math floor and the result is equally close to two integers?

If two double values that are mathematical integers are equally close, the result is the integer value that is even. Special cases: If the argument value is already equal to a mathematical integer, then the result is the same as the argument.


According to the same Javadoc:

If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument. Can't do that with an int.

The largest double value is also larger than the largest int, so it would have to be a long.


It's for precision. The double data-type has a 53 bit mantissa. Among other things that means that a double can represent all whole up to 2^53 without precision loss.

If you store such a large number in an integer you will get an overflow. Integers only have 32 bits.

Returning the integer as a double is the right thing to do here because it offers a much wider usefull number-range than a integer could.


Others have told you the why, I'm going to tell you how to round correctly given you want to do this. If you are only going to use positive numbers, then you can use this statement:

int a=(int) 1.5;

However, the (int) always rounds towards 0. Thus, if you want to do a negative number:

int a=(int) -1.5; //Equal to -1

In my case, I didn't want to do this. I used the following code to do the rounding, and it seems to handle all of the edge cases well:

private static long floor(double a)
{
    return (int) Math.floor(a);
}

What would you want it to return if you gave it a double bigger than the largest int or long?

(Admittedly if it's bigger than the largest long the precision will be low anyway - it may not be the nearest theoretical integer - but even so...)


Just as there is an integer and a floating point division in Java, there are integer and floating point ways to do floor:

double f = Math.floor(x);

or

int k = (int) x; 

but you always need to be careful with using floor with finite precision arithmetic: your calculation of x may yield something like 1.99999999 which will be floored to 1, not 2 by both forms. There are many algorithms out there that need to work around this limitation to avoid producing wrong results for some input values.