From the API docs:
public static long round(double a)
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:
(long)Math.floor(a + 0.5d)
> Math.round(-0.500001) returns the exact result of -1.
Yes I know.
double a;
a = -0.5d;
System.out.println((long)Math.floor(a + 0.5d));
a = -0.500001d;
System.out.println((long)Math.floor(a + 0.5d));
> I think it is more correct when a negative value will
> be subtracted by 1/2?
Feel free to write your own round method:public static long yourRound(double a) {
return (long)Math.floor(a + a > 0.0d ? 0.5d : -0.5d);
}
> @ roger.bagnall:
> No it will not it will return 1 as per the
> definition.
>
> Due to the implemenation (and also because the docs
> say so) every half (0.5) gets rounded to the closest
> greater whole number.
> so 0.5 gets 1 and -0.5 gets 0 (because 0 is greater
> then -1)
>
> Lima
@Lima
What I meant was Math.round(-0.5) returns the exact value of 0, as per the original post.
The argument value was a typo
> I think it is more correct when a negative value will
> be subtracted by 1/2?
That depends. If you look it up on google you will see some debate about rounding negative numbers.
Some say that if its 0.5 (or 1.5) it should be rounded upward.
Rounding upward with negative numbers would make -0.5 become 0. So you can feel free to think it should be -1 but other people will differ in whats better/more logical.
So as already sid you can always make your own round method if you think the current one isn't good enough. I even think there are standards for rounding numbers but don't know any from mind.
Lima
p.s. @ roger
Aha. just ignore my comment then :) these forums should read minds and auto-fix those typo's anyhow.
> Ok, I rename the subject of this post from
> Math.round(-0.5) returns zero to The Math
> class has a method naming problem.
>
> The Math class provides a method with the name
> round, but the method doesn't do what the
> method name says.
No, the round method does not round by your definition of rounding.
Mathematicians and numerical analysts
got together in 1985, and realized that
at different times they need different rounding definitions.
They came up with at least 4 must-haves,
and these 4 different rounding modes are now in the IEEE standard,
and is implemented as-is by every modern CPU:
http://en.wikipedia.org/wiki/IEEE_754
So... rounding is by no means as simple as you seem to think.
> No, the round method does not round by your definition of rounding.
No, the round method does not round by MATLAB's definition of rounding,
the round method does not round by Octave's definition of rounding,
and the round method does not round by the definition of every mathematical book.
From this it follows that the method name is misplaced
> Mathematicians and numerical analysts
> got together in 1985, and realized that
> at different times they need different rounding
> definitions.
>
> They came up with at least 4 must-haves,
> and these 4 different rounding modes are now in the
> IEEE standard,
> and is implemented as-is by every modern CPU:
> http://en.wikipedia.org/wiki/IEEE_754
>
> So... rounding is by no means as simple as you seem
> to think.
Miss McDonnell, Kathy if I may, I find your contributions to these forums highly informative! Hope to see you more around here!
; )
> > No, the round method does not round by your
> definition of rounding.
>
> No, the round method does not round by MATLAB's definition of rounding,
> the round method does not round by Octave's definition of rounding,
> and the round method does not round by the definition of every mathematical book.
>
> From this it follows that the method name is misplaced
Of course. Well, then you'll have to report a bug.
Did you also report the floating point bug from one of your previous posts already?
> What do you think about the following method names:
>
> Math.roundToNearest()
> Math.roundTowardZero()
> Math.roundTowardPosInf()
> Math.roundTowardNegInf()
>
> Good method names may prevent some missunderstandings.
The current round(...) method will definatelly not change, of course. However, you could file a RFE (reques for enhancement) with your suggestion(s).
Read more about it on this page:
http://bugs.sun.com/services/bugreport/index.jsp
Good luck.
> What do you think about the following method names:
> Math.roundToNearest()
What then is the result for -0.5, where 0 and -1 are as near as each other?
If you adopt the round-half-even scheme, this function is Math.rint().
> Math.roundTowardZero()
This is missing from Java's Math class. It has the disadvantage that zero absorbs a wider interval than any other integer (though the intervals for round-half-even are not uniform either).
> Math.roundTowardPosInf()
This is more commonly known as ceiling.
> Math.roundTowardNegInf()
This is more commonly known as floor.
Round towards positive infinity and towards negative infinity are the phrases more often associated with rounding of the results of operations, rather than an operation themselves, so sticking with floor() and ceil() are probably better names for the functions.
Pete
>> Miss McDonnell, Kathy if I may, I find your contributions to these forums highly informative! Hope to see you more around here!
Because I am competing for Prometheuzz's approval, lol...
>> > No, the round method does not round by MATLAB's definition of rounding,
> the round method does not round by Octave's definition of rounding,
> and the round method does not round by the definition of every mathematical book.
Here is a little known tidbit, the mod function with negative numbers
is equally "controversial". It is even incosistent between Microsoft products!
http://mathforum.org/library/drmath/view/52343.html
You cant even trust Excel ; )