等于整数的双精度数总是会转换为该整数吗?

发布于 2025-01-06 12:26:40 字数 275 浏览 1 评论 0原文

等于整数的双精度数是否总是转换为该整数(假设双精度数不是导致溢出的双精度数)。示例: Math.ceil() 将返回一个等于整数的双精度值。假设没有溢出,它是否总是会转换为它应该等于的相同整数?

如果不是,如何将 double 舍入为 intlong

Will a double equal to an integer always cast to that integer (assuming the double is not one that causes an overflow). Example: Math.ceil() will return a double that is equal to an integer. Assuming no overflow, will it always cast to the same integer that it is supposedly equal to?

If not, how can I round up a double to an int or long?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

软甜啾 2025-01-13 12:26:40

由于 Java 类型是固定的,并且 Java 双精度数具有 52 位尾数,因此它们可以(轻松地)表示32 位 Java int,不进行舍入。

Since Java types are fixed and Java doubles have a 52 bit mantissa, they can (with ease) represent a 32-bit Java int without rounding.

东京女 2025-01-13 12:26:40

是的,它会准确转换。 第 5.1.3 节 JLS,其中提到

否则,如果浮点数不是无穷大,则
浮点值四舍五入为整数值 V,四舍五入方向为
使用 IEEE 754 向零舍入模式归零...

由于您的 double 完全等于 int,因此“四舍五入”值只是完全相同的值,但您可以阅读规范了解详细信息。

Yes, it will convert exactly. This is described in Section 5.1.3 of the JLS, which mentions

Otherwise, if the floating-point number is not an infinity, the
floating-point value is rounded to an integer value V, rounding toward
zero using IEEE 754 round-toward-zero mode...

Since your double exactly equals the int, the "rounded" value is just the exact same value, but you can read the spec for details.

享受孤独 2025-01-13 12:26:40

所有可能的 int 值都可以用 double 表示,不会出现错误。最简单的舍入方法是使用 Math.ceil() 例如

double d = 
long l = (long) Math.ceil(d); // note: could overflow.

All possible int values can be represented by a double without error. The simplest way to round up is to use Math.ceil() e.g.

double d = 
long l = (long) Math.ceil(d); // note: could overflow.
攒一口袋星星 2025-01-13 12:26:40

根据经验,答案似乎是肯定的 - 请注意,它也适用于 i2 = (int) d;

public static void main(String[] args) {
    for (int i = Integer.MIN_VALUE + 1; i < Integer.MAX_VALUE; i++) {
        double d = i;
        int i2 = (int) Math.ceil(d);
        if (i != i2) {
            System.out.println("i=" + i + " and i2=" + i2); //Never executed
        }
    }
}

Empirically, the answer seems to be yes - note that it also works with i2 = (int) d;.

public static void main(String[] args) {
    for (int i = Integer.MIN_VALUE + 1; i < Integer.MAX_VALUE; i++) {
        double d = i;
        int i2 = (int) Math.ceil(d);
        if (i != i2) {
            System.out.println("i=" + i + " and i2=" + i2); //Never executed
        }
    }
}
无所的.畏惧 2025-01-13 12:26:40

我相信是这样,但你可以自己测试一下:

public static void main(String... args) throws Exception {
    int interactions = Integer.MAX_VALUE;
    int i = Integer.MIN_VALUE;
    double d = Integer.MIN_VALUE;
    long init = System.currentTimeMillis();
    for (; i < interactions; i++, d++)
        if (!(i == (int) Math.ceil(d)))
            throw new Exception("something went wrong with i=" + i + " and d=" + d + ", Math.ceil(d)="+Math.ceil(d));

    System.out.println("Finished in: "+(System.currentTimeMillis() - init)+"ms");
}

I believe so, but you might test it yourself:

public static void main(String... args) throws Exception {
    int interactions = Integer.MAX_VALUE;
    int i = Integer.MIN_VALUE;
    double d = Integer.MIN_VALUE;
    long init = System.currentTimeMillis();
    for (; i < interactions; i++, d++)
        if (!(i == (int) Math.ceil(d)))
            throw new Exception("something went wrong with i=" + i + " and d=" + d + ", Math.ceil(d)="+Math.ceil(d));

    System.out.println("Finished in: "+(System.currentTimeMillis() - init)+"ms");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文