Java should provide exact real arithmetics

Andreas Andreakis andreas.andreakis at
Wed Dec 12 09:16:45 UTC 2007


my default platform of choice usually is the jvm. But right now, I´m working
for a project, where I can´t use it. The project requires high precision,
error free and fast arithmetics with real numbers. And it turns out that
java does not fit this requirements.

BigDecimal of course is a major improvement against primitive low precision
types such as float or double, but BigDecimal does not scale and does not
implement mathematical functions such as exp, sqrt, sin, ......

Especially if you need to multiply numbers from the open interval (0,1) such
as 0.000234 * 0.4432001234 , you will reach BigDecimals performance limits
very soon.

for a demonstration execute the following loop:

int loops = 100;
BigDecimal number = new BigDecimal("0.123");

for(int i = 0; i<loops; i++){
long time = System.currentTimeMillis();
number = number.pow(2);

System.out.println("iteration: "+i+", length:
"+number.toPlainString().length()+", time: "+(System.currentTimeMillis() -
time)+" ms");

multiplying double-sized numbers requires more than double time. Working
with BigDecimal feels a little like concatinating huge String using the +

Currently, there exist much better solutions than BigDecimal, unfortunately
not from the Java space. For instance GMP ( is considered
as one of the state of the art libraries in the area of precision
arithmetics. And several libraries extent GMP such as the popular iRRAM
project (

Check out the following page in order to get an idea how performable GMP is:

it demonstrates the calculation of pi with many digits.

I´m asking myself why there is nothing comparable for the java platfom.
Several usecases require high precision and fast arithmetics, especially
from the field of scientific computing, simulations, but also financial
maths and many more. My usecase for instance is related to artificial

Why shouldn´t java have a n-math (new maths) package, implementing a
competitive solution to exact arithmetics ?

A alternative to a clean room implementation would be to integrate existing
(C or C++) solutions into the jvm ,such as GMP.

what is your opinion on that ? are facing similar limitations using

kind regards,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the core-libs-dev mailing list