Ok, since according to the comment the binary workings are requested:
In general see https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.2.4
A) double to float:
The Java programming language requires that floating-point arithmetic behave as if every floating-point operator rounded its floating-point result to the result precision.
So the double is rounded to the next float. The conversion can be described as follows:
- Keep the sign.
- cast the
signed int 11 used as exponent to a signed int 8. If this is not possible, because it causes an overflow, return Float.POSITIVE_INFINITY or Float.NEGATIVE_INFINITY.
- and discard the last 29 bit of the mantissa by rounding.
As a result you have converted the 1+11+52 bit in a double to 1+8+23 bit in a float.
B) floating point to int:
The Java programming language uses round toward zero when converting a floating value to an integer (ยง5.1.3), which acts, in this case, as though the number were truncated, discarding the mantissa bits.
So in simple terms: Write down the number (not in scientific notation) and remove everything beyond the .
In the PC it probably works by shifting the mantissa exponent bits in the appropriate direction, then discarding (not rounding!) any remaining decimal fractions. And in the end multiplying the result by -1 if the float was negative. in short:
int = float.mantissa << float.exponent * (float < 0 ? -1 : 1)
Can all of this be done by hand and thus results predicted: yes, of course, otherwise these definitions would be useless. But it requires you work with the binary-representation of the numbers you're dealing with, as e.g. "discarding digits" creates different results depended on the base.
signed int 11used as exponent to asigned int 8and discard the last 29 bit of the mantissa (rounding if necessary). As a result you have converted the 1+11+52 bit in a double to 1+8+23 bit in a float. - PoohlFloat.valueOf(double)(I'm guessing at the name of it, it should be something like that) - Nathan Loyer