How does double to int cast work in Java
Asked Answered
S

3

29

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 ?

Sashenka answered 20/9, 2012 at 14:34 Comment(2)
It truncates the decimal and returns only the whole number as the int.Cotta
amazed that this question has not been asked before.Infidelity
F
20

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

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.

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

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

Filtrate answered 20/9, 2012 at 14:38 Comment(6)
I don't really get it. Can you give an example please ? like from a very long floating point number to intSashenka
In that case, read the last sentence of the quote. You will get MAX_VALUE every time.Octopus
Sorry, I think i am not clear about the first step. Does it convert it through binary ? taking the example double x = 54.3578, and what happen at the first step ?Sashenka
@user1389813: The value is in binary to start with. At compile-time, the compiler will convert the literal 54.3578 to the closest representable IEEE-754 64-bit double value.Filtrate
@JonSkeet but how does it extract that 54 from the IEEE 754 double-precision binary floating-point format where it contains sign, exponent and fraction ?Sashenka
@user1389813: As I wrote in my answer, most of the time the CPU would do that. If the CPU doesn't support floating point directly, it would be implemented internally within the JVM, with code which understands IEEE-754... but at this point we're a long way from anything truly Java specific, and it's unclear why you'd really want to know. The effect is the important part, and that's specified in my answer·Filtrate
R
14

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

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

Redeemable answered 20/9, 2012 at 14:40 Comment(1)
i think the question is not how to do it, but the mechanics behind the castInfidelity
O
2

You may want to read the Java specification:

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

Opportunist answered 20/9, 2012 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.