Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #12284

Re: Fixed-point arithmetic library

From Jan Burse <janburse@fastmail.fm>
Newsgroups comp.lang.java.programmer
Subject Re: Fixed-point arithmetic library
Date 2012-02-24 00:02 +0100
Organization albasani.net
Message-ID <ji6gh9$1jb$1@news.albasani.net> (permalink)
References <alpine.DEB.2.00.1202222012550.5334@urchin.earth.li> <ji3ogh$i8r$1@news.albasani.net> <alpine.DEB.2.00.1202232046150.23797@urchin.earth.li>

Show all headers | View raw


You might want to implement a flexible ALU. You
could use:

    Long: To represent [-922337203685477.5808, 922337203685477.5807]
    BigDecimal: To represent everyting that is outside of the above
      range or has not a scale 4.

You can implement the ALU as static methods that take
as argument the type Number.

public class myALU {

     public Number add(Number a, Number b);
     Etc..

}

I did not yet do it. But I did something similar for
arbitrary long integers. So I was dynamically switching
between:

    Integer: To represent [2147483648, 2147483647]
    BigInteger: To represent everyting that is outside of
       the above range.

I also used Integer.valueOf() instead of new Integer(),
since the later works with a small cache. The overall
performance boost was quite visible.

I am planning to do the same for BigDecimals now. I have
picked the range [-922337203685477.5808, 922337203685477.5807]
since many databases (for example MS SQL Server) use this
range and scale for a small currency/money datatype.

Bye

Tom Anderson schrieb:
> 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
> 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.
>
> I'd still be interested in any existing fixed-point libraries, as
> another option to consider.
>
> tom
>
>> Decimal scale means your numbers would be represented as:
>>
>> mantissa * 10 ^ -scale
>>
>> Bye
>>
>> Tom Anderson schrieb:
>>
>>> I would quite like to represent some numbers in fixed point, and do
>>> arithmetic with them.
>>>
>>> These numbers will mostly not be more than a bilion, and will probably
>>> never be more than a hundred billion. Some of them, i will need to
>>> represent to eight decimal places. I'd like to be able to multiply two
>>> large numbers, but i suspect will not need to multiply three. 11 * 2 + 8
>>> = 30 decimal digits; that's about 100 bits, so 128 bits would be big
>>> enough (about 5 decimal digits of headroom).
>>>
>>> Can anyone suggest an existing library for doing fixed-point arithmetic
>>> which would cover this?
>>>
>>> I have googled, but not found anything. There are a few fixed-precision
>>> libraries aimed at J2ME (i assume because old phones didn't have
>>> floating-point units?). There's something called jExigo, but it's 64-bit
>>> and the code doesn't look great.
>>>
>>> It's quite possible that there is no such library. But i thought it
>>> prudent to ask before writing one myself.
>

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Fixed-point arithmetic library Tom Anderson <twic@urchin.earth.li> - 2012-02-22 21:39 +0000
  Re: Fixed-point arithmetic library Jan Burse <janburse@fastmail.fm> - 2012-02-22 22:59 +0100
    Re: Fixed-point arithmetic library Tom Anderson <twic@urchin.earth.li> - 2012-02-23 21:15 +0000
      Re: Fixed-point arithmetic library markspace <-@.> - 2012-02-23 13:53 -0800
      Re: Fixed-point arithmetic library Robert Klemme <shortcutter@googlemail.com> - 2012-02-23 23:21 +0100
      Re: Fixed-point arithmetic library Jan Burse <janburse@fastmail.fm> - 2012-02-24 00:02 +0100
  Re: Fixed-point arithmetic library Robert Klemme <shortcutter@googlemail.com> - 2012-02-22 23:13 +0100
    Re: Fixed-point arithmetic library Gene Wirchenko <genew@ocis.net> - 2012-02-22 14:59 -0800
      Re: Fixed-point arithmetic library Martin Gregorie <martin@address-in-sig.invalid> - 2012-02-23 01:05 +0000
        Re: Fixed-point arithmetic library Tom Anderson <twic@urchin.earth.li> - 2012-02-23 21:21 +0000
          Re: Fixed-point arithmetic library Gene Wirchenko <genew@ocis.net> - 2012-02-23 13:49 -0800
      Re: Fixed-point arithmetic library Tom Anderson <twic@urchin.earth.li> - 2012-02-23 21:16 +0000
        Re: Fixed-point arithmetic library Gene Wirchenko <genew@ocis.net> - 2012-02-23 14:03 -0800
          Re: Fixed-point arithmetic library Lew <noone@lewscanon.com> - 2012-02-23 14:42 -0800
            Re: Fixed-point arithmetic library Gene Wirchenko <genew@ocis.net> - 2012-02-23 15:08 -0800
        Re: Fixed-point arithmetic library Martin Gregorie <martin@address-in-sig.invalid> - 2012-02-23 23:05 +0000
    Re: Fixed-point arithmetic library glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-02-27 00:59 +0000
      Re: Fixed-point arithmetic library Arne Vajhøj <arne@vajhoej.dk> - 2012-02-26 20:50 -0500
        Re: Fixed-point arithmetic library Robert Klemme <shortcutter@googlemail.com> - 2012-02-27 07:32 +0100
          Re: Fixed-point arithmetic library Jan Burse <janburse@fastmail.fm> - 2012-02-27 13:46 +0100
  Re: Fixed-point arithmetic library Roedy Green <see_website@mindprod.com.invalid> - 2012-02-24 16:26 -0800

csiph-web