• 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.round()

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, my question is from poddar's mock exam. isn't Math.round(f6) supposed to return -6?
{ float f4 = -5.5f;
float f5 = 5.5f;
float f6 = -5.49f;
float f7 = 5.49f;
System.out.println("Round f4 is " + Math.round(f4));
System.out.println("Round f5 is " +Math.round(f5));
System.out.println("Round f6 is " + Math.round(f6));
System.out.println("Round f7 is " + Math.round(f7)); }
ans: Round f4 is -5 Round f5 is 6 Round f6 is -5 Round f7 is 5
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even I have faced this doubt.
Can somebody clarifies!!!
Thanx
Rajani
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's how the round() method is in the source
<pre>
public static int round(float a) {
return (int)floor(a + 0.5f);
}
</pre>
And here's what the floor method does(since it's native refer to API)
<pre>
floor
public static double floor(double a)
Returns the largest (closest to positive infinity) double
value that is not greater than the argument and is equal to a mathematical integer.
Parameters:
a - a double value.
a - an assigned value.
Returns:
the largest (closest to positive infinity) double value that is not greater than the argument and is equal
to a mathematical integer.
</pre>
So to estimate the rounded off value we add 0.5 to our number & then get the largest double value that is not greater than the argument & is equal to a maths integer . For instance
Math.round(-5.49f)
Add 0.5 ---> -5.49 + 0.5 = -4.99
Get the largest double value
that's a maths integer
but not greater than the argument ----> -5
So , Math.round(-5.49) = -5

Correct me if I'm wrong ( First time i've looked at the source)
Ashish
[This message has been edited by Ashish Hareet (edited July 29, 2001).]
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Ashish.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, here is my method:

For round() method,
any number falling inside -5.5(inclusive)~-5.0 will get -5.0
any number falling inside +5.5(inclusive)~+6.0 will get +6.0
notice that the result is to positive x axis direction.
similarly,
any number falling inside -6.0~-5.5(exclusive) will get -6.0
any number falling inside +5.0~+5.5(exclusive) will get +5.0
notice that the result is to negative x axis direction.
Hope it helps.
Guoqiao

[This message has been edited by Guoqiao Sun (edited July 29, 2001).]
 
kavita desai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot ashish and guoqiao.
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
the round meth returns an int or long depending whether u pass a float or double as the argument.It adds 0.5 to the argument and returns the int closest to it.So -5.49 will return -5 and -5.7 will return -6.
 
We're being followed by intergalactic spies! Quick! Take 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