programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Liutauras Vilda
• Henry Wong
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Mikalai Zaikin
• Himai Minh

# Clarify the output please . . .

Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
Guys, Can you please tell me why I get this output from the code below.
Yikes. a and b are equal
Nope. a and c are not equal
----------------------------------------------------------------
import java.math.*;
public class Q1 {
public static void main(String[] args)
{
double a = Math.ceil(Double.MIN_VALUE);
double b = Math.ceil(Double.MIN_VALUE + 1);
double c = Math.ceil(Double.MIN_VALUE + 2);
if (a == b)
System.out.println("Yikes. a and b are equal");
else
System.out.println("Nope. a and b are not equal");
if (a == c)
System.out.println("Yikes. a and c are equal");
else
System.out.println("Nope. a and c are not equal");
}
}

Anonymous
Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
Double.MIN_VALUE is the smallest positive value a double variable can hold, which is 4.9E-324.
( I think, 4.9E-324 = (4.9/(Math.pow(10,324))
Math.ceil(Double.MIN_VALUE) is equal to 1.0. [because ceil returns the smallest double value that is not less than the argument and is equal to a mathematical integer.]
Math.ceil(Double.MIN_VALUE + 1) is equal to 1.0, because if you add Double.MIN_VALUE to any variable, it does not affect the value of that variable.(May be because Double.MIN_VALUE is so negligible. I dont know why, but this is what it does).
double d=1.0;
d=d+Double.MIN_VALUE;
System.out.println(d); // prints 1.0, not 1.0 + 4.9E-324
That is why
Math.ceil(Double.MIN_VALUE)==Math.ceil(Double.MIN_VALUE + 1);
is true.
Because both operands evaluate to be 1.0
- Srini

Anonymous
Ranch Hand
Posts: 18944
• Number of slices to send:
Optional 'thank-you' note:
Thanks Srini for the explanation.
I understand it better now, though I fail to understand why Math.ceil(some_integer + Double.MIN_VALUE (which is another positive value)) is still some_integer.
But I will just remember this point for the exam.
Thanks once again !!!
Regards,
Shashank

Ranch Hand
Posts: 116
• Number of slices to send:
Optional 'thank-you' note:
Any value less than 1e-16 is being ignored. Is it a bug?