gpt4 book ai didi

Java - 误解 ceil 和 Floor 方法

转载 作者:行者123 更新时间:2023-11-30 02:02:23 24 4
gpt4 key购买 nike

楼层:

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. ...

天花板:

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. ...

来源:Docs Oracle

关于floor:如果我输入 System.out.print(Math.floor(2.1)); 返回 2.0 。其他示例: System.out.print(Math.floor(2.8)); 返回 2.0 。我将用这个例子来论证这个描述:如果 floor(2.1) 是最大的(最接近正无穷大),结果将是 3.0 而不是 2.0 ,因为我认为 2.0 最接近负无穷大。因此,如果我更改有关楼层的描述:

Returns the smallest (closest to negative infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. ...

这对我来说很有意义,我会理解 floor(2.1) 返回 2.0

当我读到“最接近正无穷大”和“最接近负无穷大”时,我认为在数轴中:

The number line

来源:Quora

编辑:我要问的是:描述让我心碎。我的逻辑是这样的(例如关于地板):首先,好吧,当我听地板时,我认为是最小的而不是最大的。其次,如果我返回最大的值,则该值大于或等于参数。 ceil 也会发生同样的情况

最佳答案

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

关键在于短语小于或等于参数

因此 2.0 是小于或等于2.1 且也等于整数值的最大 double 值。

ceil 也是如此:描述提到了大于或等于输入值的最小值...

所以,原来的描述实际上是正确的。

关于Java - 误解 ceil 和 Floor 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52417606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com