Re: Fixed-point arithmetic library
On 02/23/2012 10:15 PM, Tom Anderson wrote:
On Wed, 22 Feb 2012, Jan Burse wrote:
If a decimal scale suits you, you could use:
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
Decimal would be fine. BigDecimal might actually be okay too.
I've actually misrepresented the context for this question slightly. My
current place of work does a few calculations, and we currently use a
"few" like in "a few millions per second"?
fixed-point implementation of our own. However, it doesn't quite meet
all our needs. My choice is really between extending it, and replacing
it with something else. Being lazy, i would rather take advantage of
someone else's hard work than do any myself.
Now, we wrote this fixed-point implementation having previously used
BigDecimal, because we had some serious performance problems with that.
This was before my time, so i'm very hazy on the details. I had already
dismissed BigDecimal out of hand on the basis of that, but it might
actually be worth looking at again.
How long is that benchmark ago? JVMs and standard library have changed
quite a bit during past years so the performance issues might actually
have gone by now.
If I would be the one responsible I'd do the measurements myself. It's
usually better to base such decisions on hard (and current) facts. Your
application obviously exists so you have a clear idea of the nature of
operations you have to perform as well as the frequency of operations as
well as the range of input data. That should make it quite easy to
build a benchmark which realistically reflects your business case. You
could separate the load driving from the used math implementation via
interface (see minimalistic example below) so you can reuse the same
tests for all the implementations you want to analyze (at least
BigDecimal and what you built).
Kind regards
robert
package clj.math;
/**
* Abstraction of math impl.
*
* @param <T>
* type of math numbers
* @author robert
*/
public interface Math<T> {
/**
* Create a new number from int.
*
* @param i
* an integer to be converted.
* @return a number object representing the int.
*/
T create(int i);
/**
* Create a new number from String.
*
* @param s
* input string, must be a decimal representation of a number.
* @return a number object representing the string.
*/
T create(String s);
/**
* Addition of two numbers.
*
* @param a
* a number
* @param b
* a number
* @return a + b
*/
T plus(T a, T b);
/**
* Multiplication of two numbers.
*
* @param a
* a number
* @param b
* a number
* @return a * b
*/
T mult(T a, T b);
}