在 Java 中,double 到 int 的转换是如何工作的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12514958/
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-10-31 09:11:56  来源:igfitidea点击:

How does double to int cast work in Java

javacastingprimitivemantissa

提问by peter

I am new to Java, and wondering how does double to int cast work ? I understand that it's simple for long to int by taking the low 32 bits, but what about double (64 bits) to int (32 bits) ? those 64 bits from double in binary is in Double-precision floating-point format (Mantissa), so how does it convert to int internally ?

我是 Java 新手,想知道 double 到 int cast 是如何工作的?我知道通过采用低 32 位将 long 转换为 int 很简单,但是 double (64 bits) to int (32 bits) 呢?来自 double in binary 的 64 位是双精度浮点格式(尾数),那么它如何在内部转换为 int 呢?

采纳答案by Jon Skeet

It's all documented in section 5.1.3of the JLS.

这一切都记录在JLS 的第 5.1.3 节中。

In the first step, the floating-point number is converted either to a long, if T is long, or to an int, if T is byte, short, char, or int, as follows:

If the floating-point number is NaN (§4.2.3), the result of the first step of the conversion is an int or long 0.

  • 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 (§4.2.3). Then there are two cases:

    • If T is long, and this integer value can be represented as a long, then the result of the first step is the long value V.

    • Otherwise, if this integer value can be represented as an int, then the result of the first step is the int value V.

  • Otherwise, one of the following two cases must be true:

    • The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long.

    • The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.

第一步,将浮点数转换为 long(如果 T 是 long)或转换为 int(如果 T 是 byte、short、char 或 int),如下所示:

如果浮点数是 NaN(第 4.2.3 节),则转换的第一步的结果是 int 或 long 0。

  • 否则,如果浮点数不是无穷大,浮点值将被舍入为整数值 V,使用 IEEE 754 向零舍入模式(第 4.2.3 节)向零舍入。那么有两种情况:

    • 如果T是long,并且这个整数值可以表示为long,那么第一步的结果就是long值V。

    • 否则,如果这个整数值可以表示为一个int,那么第一步的结果就是int值V。

  • 否则,以下两种情况之一必须为真:

    • 该值必须太小(大的负值或负无穷大),并且第一步的结果是 int 或 long 类型的最小可表示值。

    • 该值必须太大(大的正值或正无穷大),并且第一步的结果是 int 或 long 类型的最大可表示值。

(The second step here is irrelevant, when Tis int.)

(这里的第二步无关紧要,什么时候Tint。)

In most cases I'd expect this to be implementedusing hardware support - converting floating point numbers to integers is something which is usually handled by CPUs.

在大多数情况下,我希望这可以使用硬件支持来实现- 将浮点数转换为整数通常由 CPU 处理。

回答by arutaku

Java truncates its value if you use (int) cast, as you may notice:

如果您使用 (int) 强制转换,Java 会截断它的值,您可能会注意到:

    double d = 2.4d;
    int i = (int) d;
    System.out.println(i);
    d = 2.6;
    i = (int) d;
    System.out.println(i);

Output:

输出:

2
2

Unless you use Math.round, Math.ceil, Math.floor...

除非你使用 Math.round、Math.ceil、Math.floor...

回答by mikera

You may want to read the Java specification:

您可能需要阅读 Java 规范:

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3

The relevant section is 5.1.3 - "Narrowing Primitive Conversion".

相关部分是 5.1.3 - “Narrowing Primitive Conversion”。