wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Math function doubt 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 function doubt" Watch "Math function doubt" New topic
Author

Math function doubt

Vidyavathi saravanan
Ranch Hand

Joined: Sep 24, 2004
Posts: 34

Answer is given as -5,6,-5,5.
My doubt is the 3 rd output.
Math.round(-5.49f)--- how come this is -5. As per my understanding it is -6.
Could anyone please explain this. Its confusing me a lot.


Thanks.
Nina Binde
Ranch Hand

Joined: Sep 24, 2004
Posts: 85
float f6 = -5.49f;

round always rounds a float or a double to the nearest integer or a long respectively. If it is right in the middle like 5.5, it rounds to the nearest large integer.
For the above example, -5.49 is closer to -5 than it is to -6. Hence , the output.

The answer would be -6 if f6 = -5.51f.

Get it?
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Vidyavathi,
If you look the underlying implementation of round , it is nothing but adding 0.5f to the number and then perform floor on the result. In your case,

I know this round function is confusing sometimes, but if you know floor method correctly then you will never fail in getting answer for round function. Hope this helps ya.
[ November 11, 2004: Message edited by: Jay Pawar ]

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Math function doubt