patch ping Re: [java] Fix BigDecimal ROUND_HALF_EVEN

Mark Wielaard
Fri Aug 1 12:58:00 GMT 2003


On Sun, 2003-07-20 at 00:23, Per Bothner wrote:
> Mark Wielaard wrote:
> > You need half also to see if ROUND_UNNECESSARY can be satisfied.
> I don't understand this.

Sorry, ignore me. That was with the old code. The new version has
ROUND_UNNECESSARY optimized so that it doesn't use half at all.

> >>This (in the original code) looks questionable:
> >>     int sign = unrounded.signum ();
> >>Consider the test in ROUND_CEILING:
> >>	roundingMode = (sign == 1) ? ROUND_UP : ROUND_DOWN;
> >>Shouldn't this be whether the this.signum() is positive?
> >>Consider dividing -9 by 10.  unrounded.signum () is 0,
> >>so we round down when we should be rounding up.
> > 
> > In the above case unrounded.signum () would be -1.
>  > I believe the original is correct.
> Ok, consider -9 divided by 100 instead (with all scales zero).

Aha. I see what you are getting at. Indeed unrounded.signum() is the
wrong sign. But this.signum() is also not correct in for example
9 divided by -100. So (since the zero cases are already taken care of)
sign should actually be intVal.signum () * valIntVal.signum ().
Fixed and an explicit test added to Mauve.

> >>Have you tested it?
> > 
> > There already were some 600 BigDecimal tests for Mauve. I added a couple
> > more for some ROUND_HALF_EVEN corner cases (positive/negative, odd/even,
> > different nearest neigbors or equidistant). All these tests pass with
> > the patch below.
> Do you test negative values of val as well?

I have now added tests for all forms of divide (pos/neg, different
rounding modes and scales) to Mauve. There was one additional thing to
fix in the calcultation of half (both values should be positive before
comparing). Also fixed. All 164 new cases now succeed.

> > OK to commit?
> Yes, but may I suggest some minor optimizations?

All added. Final patch as committed attached.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: BigDecimal.patch2
Type: text/x-patch
Size: 4242 bytes
Desc: not available
URL: <>

More information about the Java mailing list