• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Math.ceil(double)

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q24.
Q. What will be the output of follwing
{
double d1 = -0.5d;
System.out.println("Ceil for d1 " + Math.ceil(d1));
System.out.println("Floor for d1 " +Math.floor(d1));
}
Answers:
a) Ceil for d1 0
Floor for d1 -1;
b) Ceil for d1 0
Floor for d1 -1.0;
c) Ceil for d1 0.0
Floor for d1 -1.0;
d) Ceil for d1 -0.0
Floor for d1 -1.0;
The answer mentioned is (d).
How is calculating (d) instead of (c)?
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is because float and double have -0.0 and +0.0
not like the other primatives which only have 0. you need to read about floating point numbers and NAN.
 
Savio Mascarenhas
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Randall Twede:
it is because float and double have -0.0 and +0.0
not like the other primatives which only have 0. you need to read about floating point numbers and NAN.


which is best & clearly put forward source for this info ???
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the reason the answer is -0.0 is because the signed bit, since ceil operation doesn't change the signed bit, you get a result that is -0.0.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Savio,
One place to look is the JDK. Here's what says for Math.ceil()


If the argument value is less than zero but greater than -1.0,
then the result is negative zero.


To find out more about how Java handles floating-point numbers in general, check the JLS§4.2.3 and 4.2.4
You can view them both on-line or download them from http://java.sun.com/docs/index.html
Hope that helps.

------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Savio Mascarenhas
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jane Griscti:
Savio,
One place to look is the JDK. Here's what says for [b]Math.ceil()

To find out more about how Java handles floating-point numbers in general, check the JLS�4.2.3 and 4.2.4
You can view them both on-line or download them from http://java.sun.com/docs/index.html
Hope that helps.
[/B]



Thanks Jane !!!
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SAVIO,
FLOOR : IT GETSS THE LOWER NUMBER IN THE NUMBER SCALE
CEIL : IT GETS THE HIGHER NUMGER IN THE NUMBER SCALE.
double d1 = -0.5d;

douuble d2 = 0.5d

Math.ceil(d1)...// -0 becos it is next highest number in the number scale for -0.5;
Math.ceil(d2)...// 1 becos it is the next highest number in the number scale for 0.5
Math.floor() fetch us the next lowest number ub the number scale
Jaya Murugan
 
Popeye has his spinach. I have this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic