Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.albasani.net!.POSTED!not-for-mail From: Jan Burse Newsgroups: comp.lang.java.programmer Subject: Re: Fixed-point arithmetic library Date: Fri, 24 Feb 2012 00:02:04 +0100 Organization: albasani.net Lines: 93 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net e/CAabYWjGwKsjOy92oXnqmBHNhybKa1OrE4D2YOdbYJdgBvBc/6afSD7MilgKbZpyjLsUs5dm2XyHgxEqr2ujHfmsYC23gbo3tn7hqUPuV267rT9trszo3MBQ1djlvu NNTP-Posting-Date: Thu, 23 Feb 2012 23:02:01 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="fH8J1ocxFTbO1e65IPKcRQ/k1lT/8szdBX0FWMcazyUitce319Cm1JXBBtsU4kza0lO6tVvSmRFWa9boMfLq1V0nFWPbQALyuAz12JCvabMIxkvDz+ZXRQJfN3qvW95U"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20120216 Firefox/10.0.2 SeaMonkey/2.7.2 In-Reply-To: Cancel-Lock: sha1:iV/xoidYgSiLMGV+1gD/GeuLP5Y= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:12284 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. >