Java should provide exact real arithmetics
Andreas Andreakis
andreas.andreakis at googlemail.com
Wed Dec 12 09:16:45 UTC 2007
Hi,
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 +
operator.
Currently, there exist much better solutions than BigDecimal, unfortunately
not from the Java space. For instance GMP (http://gmplib.org/) 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 (http://www.informatik.uni-trier.de/iRRAM/).
Check out the following page in order to get an idea how performable GMP is:
http://gmplib.org/pi-with-gmp.html
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
intelligence.
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
BigDecimal
kind regards,
Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20071212/6e967c66/attachment.html>
More information about the core-libs-dev
mailing list