File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# MAX_VALUE questions?

Joe Li
Greenhorn

Joined: Nov 02, 2003
Posts: 5
Hi pros,
Are there any questions in real test related class MAX_VALUE, MIN_VALUE, POSITIVE_INFINITY and NAGETIVE_INFINITY in class Float or Double?
Can anyone explain the result of following code?
class Confussion{
public static void main(String[] args){
System.out.println(Double.MAX_VALUE - 1000 - Double.MAX_VALUE);
}
}
It will print 0.0
Thank you.
Eddie Long
Ranch Hand

Joined: Nov 02, 2003
Posts: 69
The value of Double.MAX_VALUE - 1000 is a very large value! almost equivalent to Double.MAX_VALUE itself. I assume you hardly notice the difference when Double.MAX_VALUE - 1000 is evaluated. It's like picking a grain of sand from the beach and expecting ranger to notice that something's missing
In short "Double.MAX_VALUE - 1000 - Double.MAX_VALUE" is almost equal to
Double.MAX_VALUE - Double.MAX_VALUE which is in turn 0

...and so help me God.
Joe Li
Greenhorn

Joined: Nov 02, 2003
Posts: 5
Thank you.
Do you know how big the "sand" or a stone can have effect to Double.MAX_VALUE?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Joe.
Double.MAX_VALUE is so big that the JVM does not tell the difference between it and Double.MAX_VALUE-1000.
This code, however, prints 1000, thanks to the use of the class BigDecimal.

This code shows the difference between Double.MAX_VALUE and the previous bigger number representable in double:

Thus if you subtract a number fewer than "1.9958403095347198E292" from Double.MAV_VALUE the result is still Double.MAX_VALUE. This is shown by the third print statement.