Why does adding a value to Float.MAX_VALUE not reach infinity?
According to the standard, overflow in Java is handled using a special value called infinity, but here the sum is 3.4028235E38. Why is this the case? public class FloatingPointTest { public static void main(String[] args) { float a = Float.MAX_VALUE; float b = 1_000_000_000f; // Note the 'f' to ensure it's a float literal float sum = a + b; System.out.println("Float.MAX_VALUE: " + a); System.out.println("Large number: " + b); System.out.println("Sum: " + sum); System.out.println("Is sum equal to Float.POSITIVE_INFINITY? " + (sum == Float.POSITIVE_INFINITY)); } }
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.