• 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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hi,
here is one mock exam question.please see am i right or wrong?
Double a = 90.7;
Double b = method(a);
System.out.println(b);
What could be the method(b) to printout 90.0
1. abs()
2. round()
3. floor()

4. ceil()
i choose 2,3 ,but the answer is only 3 .How 2 is not correct,
my feeling is round(90.7) will return integer value 90, later casting in to double which becomes 90.0
am i wrong?
please rectify me
--thanks
shivaram
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sivram.
Only floor() will give you 90.0 . round() WILL NOT give 90.0 because floor() will return the CLOSEST LONG to the argument.There is a simple rule to evaluating this(also explained in Sun's docs):
1)add 0.5d to the argument.In your case it will give 91.2.
2)take floor() of the result.This will give you 91.
3)now cast result to long.

------------------
Come on in !! Drinks are on the house in the Big Moose Saloon !!
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class RoundTry {

public static void main(String args[]){

System.out.println("round(90.7) = " + Math.round(90.7));
}
}
it prints out 91 (far from 90.0, not even 91.0)
because java.Math.round() return long, not float or double, so why not check it up in API and play with it.
rong chen
[This message has been edited by Rong Chen (edited October 06, 2000).]
 
Rong Chen
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
by the way, round() can't take Double argument, it only takes flout or double. following lines cause compiler error:
Double a = 90.7;
round(a);
 
See ya later boys, I think I'm in love. Oh wait, she's just a 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