Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Infinities in Java

Tags:

java

What does the following expression return in Java?

Math.max(Float.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);

I saw this question in a website and the answer is Double.POSITIVE_INFINITY. I'm not sure about this answer as how can we compare 2 infinities? Can someone clarify this? Thanks.

like image 589
DonX Avatar asked Dec 01 '08 12:12

DonX


People also ask

Can you use infinity in Java?

We can only store integer numbers that fit in the memory location that we chose. For real numbers, we also have the concept of infinity, either positive or negative. The 32 bits float type and also the 64 bits double type supports this in Java.

How do you find infinity value in Java?

The java. lang. Double. isInfinite() method of Java Double class is a built in method in Java returns true if this Double value or the specified double value is infinitely large in magnitude, false otherwise.

What does infinity mean in Java?

Answer. + 3. Double. POSITIVE_INFINITY is the infinity equivalent in Java and it represents a theoretical highest number.


2 Answers

Float.POSITIVE_INFINITY returns float and Double.POSITIVE_INFINITY returns double.

There is no method called Math.max(float, double). only Math.max(float, float) and Math.max(double, double)

Therefore when the method is called Math.max(float, double), it converts the float argument to double and so the Math.max(double, double) is called so Double.POSITIVE_INFINITY is returned.

Java does not convert from double to float since it may lead to precision problem.

like image 171
sv_in Avatar answered Oct 13 '22 01:10

sv_in


Certainly you can compare infinities. Unless you get into transfinite numbering systems where there are varying degrees of infinity, infinity means just what it says, a number without limit.

The maximum/sum/product/average of two numbers without limit is one number without limit.

like image 27
paxdiablo Avatar answered Oct 13 '22 01:10

paxdiablo