Java 为什么 Math.floor 返回双精度值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/511921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:35:06  来源:igfitidea点击:

Why does Math.floor return a double?

javamathtypes

提问by Raibaz

Official Javadoc saysthat Math.floor()returns a doublethat is "equal to a mathematical integer", but then why shouldn't it return an int?

官方的JavadocMath.floor()回报double就是“等于某个整数”,但为什么不应该它返回一个int

采纳答案by krosenvold

According to the same Javadoc:

根据同一个 Javadoc:

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

如果参数是NaN无穷大或正零或负零,则结果与参数相同。不能用int.

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

最大的double价值也比最大的大int,所以它必须是一个long

回答by Nils Pipenbrinck

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.

是为了精度。double 数据类型具有 53 位尾数。除其他外,这意味着双精度数可以表示最多 2^53 的所有整数而不会丢失精度。

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

如果将如此大的数字存储在整数中,则会出现溢出。整数只有 32 位。

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.

将整数作为双精度返回是正确的做法,因为它提供了比整数更广泛的有用数字范围。

回答by Adam Davis

So that error and other non integer values can correctly cascade through a series of calculations.

这样错误和其他非整数值可以通过一系列计算正确级联。

For instance, if you feed Not a Number (NaN) into Math.floor, it'll pass it along.

例如,如果您将 Not a Number (NaN) 输入 Math.floor,它会传递它。

If it returned integer it couldn't pass these status or errors along, and you could get bad results from an earlier calculation that look good but are wrong after further processing.

如果它返回整数,则它无法传递这些状态或错误,并且您可能会从早期的计算中得到错误的结果,这些结果看起来不错,但在进一步处理后却是错误的。

回答by Jon Skeet

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

如果你给它比最大的 int 或 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...)

(诚​​然,如果它大于最大长度,则无论如何精度都会很低 - 它可能不是最接近的理论整数 - 但即便如此......)

回答by Pablo

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

就像Java中有整数和浮点除法一样,有整数和浮点的方式来做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.

但是在使用具有有限精度算术的 floor 时,您总是需要小心:您对 x 的计算可能会产生类似 1.99999999 的结果,这两种形式都会被定为 1,而不是 2。有许多算法需要解决此限制,以避免对某些输入值产生错误的结果。

回答by PearsonArtPhoto

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) 总是向 0 舍入。因此,如果你想做一个负数:

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);
}