Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MAX_VALUE questions?

 
Joe Li
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joe Li
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
Do you know how big the "sand" or a stone can have effect to Double.MAX_VALUE?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Li
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
The sky is clear now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic