• 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 class

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

K&B book says ceil(), floor() and round() all return integer equivalent floating-point numbers. But the following code prints

System.out.println(Math.round(1.1)); //prints 1
System.out.println(Math.ceil(1.1)); // prints 2.0

The first line should print 1.0, correct? Thanks.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The two signatures of round() are:
public static int round(float)
public static long round(float)

This is something very important to memorize for the exam because it will be on it:
round(),ceil(), and floor() take floats and return "integer equivalents". ceil() and floor() actually return a double.

Good Luck!
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chitra AP:
Hi All,

K&B book says ceil(), floor() and round() all return integer equivalent floating-point numbers. But the following code prints

System.out.println(Math.round(1.1)); //prints 1
System.out.println(Math.ceil(1.1)); // prints 2.0

The first line should print 1.0, correct? Thanks.



Chitra,
Math.round() returns int or long depending on the argument passed to it. i.e
Math.round(double d) will return long whereas Math.round(float f) would return you int data type.
Math.ceil(double d) always returns double.

Check this link for Math functions.
Hope that helps...
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any body pls explain some trick to find round() of negative no..

sometimes i got confuse

i m good for solving positive ones... like add .5 and if no. after decimal is =>.5 then ciel() else floor()

tell something about negative no. and some eg.

thanx
 
Whitney M. Davis
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I defer to Shweta...
[ June 09, 2005: Message edited by: Whitney M. Davis ]
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Whitney M. Davis:
Hi Amit,

So the rule for round() is you add 0.5 to the argument and then cut off whatever is after the decimal. So if you start with round(3.2), you add 0.5 and get 3.7, then cut off everything after the decimal to get 3.




ya... i got it simple...
take that wise

if in negative no. is =<.5 do ciel() else floor
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Whitney ,

So when you add 0.5 to round(-3.2) you intuitively think you should get -3.7 but you really get -2.7 and the answer will be -2. Confusing! And it'll be on the test!


I think you are wrong here.round(-3.2) is not -2 but it is -3.

Amit,
Irrespective of the number being positive or negative you have to add 0.5 to the number and then do Math.floor().
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic