this code
System.out.println(Double.MAX_VALUE+12345 == Double.MAX_VALUE);
System.out.println(Integer.MAX_VALUE+12345 == Integer.MAX_VALUE);
returns
true
false
Please clarify this difference.
The rules are the same, it's just that Double.MAX_VALUE
is so large that 12345
is roughly 300 orders of magnitude (10 300 times) smaller. Adding a number that is so much smaller than Double.MAX_VALUE
does not change its value. Adding a number that is of the same order of magnitude would make a difference, though:
Double.MAX_VALUE + 1E300
produces a positive infinity result, which is not the same as Double.MAX_VALUE
( demo )
The behavior of addition to the maximum value differs between int and double in two very significant ways:
Double.MAX_VALUE
and the next double smaller than it is about 2e292. The absolute value of the difference between 1.0 and the next double smaller than it is about 1.1e-16. Integer.MAX_VALUE
wraps around to Integer.MIN_VALUE
. Adding a big enough positive number to make a difference to Double.MAX_VALUE
results in Double.POSITIVE_INFINITY
, represented by one of the reserved bit patterns. int
is based on 2's complement binary arithmetic. It is designed to be compact, efficient, and to allow very fast simple arithmetic.
double
has more of a bias towards functionality. Often, floating point arithmetic takes more than one machine cycle. It can afford to have reserved bit patterns, such as the infinities, that require special handling.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.