java - Regarding float type precision -
i can't understand why this
float f = integer.max_value; system.out.println(integer.max_value); system.out.println((int)f);
produces same lines,
as why
float f2 = (float) integer.max_value; system.out.println(integer.max_value); system.out.println(f2.intvalue());
i mean, mantissa length floating point number 2^23-1
. how manage keep max_value of integer, 2^31 - 1
?
how manage keep max_value of integer, 2^31 - 1?
it doesn't. value of f
2147483648
.
however, narrowing primitive conversion float
int
clamps value. gets part:
otherwise, 1 of following 2 cases must true:
the value must small (a negative value of large magnitude or negative infinity), , result of first step smallest representable value of type int or long.
the value must large (a positive value of large magnitude or positive infinity), , result of first step largest representable value of type int or long.
you can see making number bigger:
float f = integer.max_value; f = f * 1000; system.out.println(integer.max_value); // 2147483647 system.out.println((int)f); // 2147483647
or casting long
instead, doesn't need clamped @ same point:
float f = integer.max_value; system.out.println(integer.max_value); // 2147483647 system.out.println((long)f); // 2147483648
Comments
Post a Comment