What’s the difference between Float.POSITIVE_INFINITY and Float.MAX_VALUE?

What’s the difference between Float.POSITIVE_INFINITY and Float.MAX_VALUE? Which is greater? Are they the same?

I came accross them looking for a value that would be greater than every other float or failing that all except the greatest. Does either meet that criteria?

Thanks!

Answer

No, they’re not the same thing at all.

Float.MAX_VALUE is the largest finite value that can be represented in a float. You won’t find any value greater than that, other than infinity. But you can perform all kinds of other operations on it.

Float.POSITIVE_INFINITY is, well, infinity. Most operations involving an infinity will end up with infinity (either positive or negative).

For example:

public class Test {
    public static void main(String[] args) {
        testOperations(Float.MAX_VALUE);
        testOperations(Float.POSITIVE_INFINITY);
    }

    public static void testOperations(float input) {
        System.out.println("input: " + input);
        System.out.println("input / 100: " + input / 100);
        System.out.println("input * 100: " + input * 100);
        System.out.println("-input: " + (-input));
        System.out.println();
    }
}

Output:

input: 3.4028235E38
input / 100: 3.4028236E36
input * 100: Infinity
-input: -3.4028235E38

input: Infinity
input / 100: Infinity
input * 100: Infinity
-input: -Infinity

Leave a Reply

Your email address will not be published. Required fields are marked *