Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Math.round() returns what ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Math.round() returns what ?" Watch "Math.round() returns what ?" New topic
Author

Math.round() returns what ?

Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
I have a problem,
I studied using the Khalid Mughal book. This book says that the Math.round() function returns an int.

However, while testing myself on MindQ's mock exam, I came across a question which says, What method(s) from the java.lang.Math class might method() be if the statement
method( -4.4 ) == -4; is true ?

The options were : round(), min(), trunc(), abs(), floor(), ceil().

I marked round() according to what Khalid's book says. But the answer is round(), floor() and ceil() !!

Please help...


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Joseph Clark
Ranch Hand

Joined: Sep 10, 2005
Posts: 48
Math.floor(-4.4) returns -5.0, which the '==' operator will narrowly convert to int when compared against -4 (and will result in a false for the comparison).

Math.ceil(-4.4) returns -4.0, which will return true when compared against -4.

Math.round(-4.4) returns (long) -4, which will be true for the comparison.*




. . . will output . . .

Math.ceil(-4.4))==-4 is true
-5.0
Math.round(-4.4))==-4 is true


*Correction: -4.4 is a double, so, Math.round(-4.4) returns the 64-bit long integer instead of the int type integer.
[ September 11, 2005: Message edited by: michael clark ]
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
and well Math.round() returns an int when a float is passed and returns a long when double is passed....


/** Code speaks louder than words */
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,
Docs says about round that..



Special cases:

  • If the argument is NaN, the result is 0.
  • If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.
  • If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.

  • Sandy


    Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
     
    GeeCON Prague 2014
     
    subject: Math.round() returns what ?