Friday, February 02, 2007

BigDecimal operator support in Java 7

One of the candidate items for Java 7 helpfully listed by Alex Miller is BigDecimal operator support. I'm keen to see this go in as it would really help customers who use decimals.

Mike Cowlishaw's General Decimal Arithmetic page is a rich source of information on this subject. It contains an example 'telco' benchmark coded in Java and C# which provide a helpful comparison when thinking about syntax support for BigDecimal.

Take the following sequence (massaged a little for display here) from the Java version:


b = p.multiply(basetax);
b = b.setScale(2, BigDecimal.ROUND_DOWN);
sumB = sumB.add(b);
t = p.add(b);

if (calltype != 0) {
d = p.multiply(disttax);
d = d.setScale(2, BigDecimal.ROUND_DOWN);
sumD = sumD.add(d);
t = t.add(d);
}

and compare the C# version for readability:

b = p * basetax * 100;
b = Decimal.Truncate(b) / 100;
sumB = sumB + b;
t = p + b;

if (calltype != 0) {
d = p * disttax * 100;
d = Decimal.Truncate(d) / 100;
sumD = sumD + d;
t = t + d;
}

Enough said?

5 comments:

Ivan said...

aehm... how about just operator overloading in java?

or is it a untouchable, sacral topic? ;)

Glyn said...

General operator overloading tends to be frowned upon by the Java community. It's thought to be one of those features of C++ that Java did well to avoid because it gives too much power for average programmers to shoot themselves, and others, in the foot.

That said, one way of providing good syntax for BigDecimals would be to introduce a limited form of operator overloading. But best to think of that as an implementation mechanism rather than adding operator overloading capability to the language for all to use.

Ivan said...

it's right, but a programming language shouldn't be optimized for average programmers, i think.

don't you think, java is in danger, compared with c#?

c# implements lots of that 'advanced programming techniques' that java avoids.

i like java very much. it's sad to see microsoft overtaking java with it's .net

Glyn said...

C# is certainly ahead of Java in its decimal syntax, which is one of the reasons I'd like Java to support proper decimal syntax.

I'm personally ambivalent about general operator overloading. It'd be handy when I need it, but I wouldn't like to have to support code that abused it.

mklemm said...

I'd like to see limited, but generic operator load in Java syntax. I've been a java programmer for many years, and additionally I've been a .NET and C# programmer long enough to experience the advantages and disadvantages of either.
IMHO there should be operator overloading tied to a specific pattern by introducing one or more interfaces for most common operator overload scenarios. This way, it could be enforced at compile time, that, if one overloads e.g. the "==" operator, you also have to provide a suitable implementation of "!=", "<", ">", ">=", "<=", "compareTo"...
This could go along with a built-in unit test suite to check for certain constraints of this implementation, like transitiveness, distributiveness, associativeness ...

That said, I don't think .NET is overtaking Java technically. There are far too many flaws in C#'s general design, as well as the mess in the standard class library...

Projects

OSGi (130) Virgo (59) Eclipse (10) Equinox (9) dm Server (8) Felix (4) WebSphere (3) Aries (2) GlassFish (2) JBoss (1) Newton (1) WebLogic (1)

Blog Archive