Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!news-1.dfn.de!news.dfn.de!news.informatik.hu-berlin.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Fixed-point arithmetic library Date: Wed, 22 Feb 2012 23:13:06 +0100 Lines: 67 Message-ID: <9ql7jpF3mnU1@mid.individual.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net J1mIMs6khgo1kpdiFXzYHA8V268AG5EVLdhVK3tBvCqOtCV7k= Cancel-Lock: sha1:bOnfLgr1vVfEI0MGlhrsAfsh36w= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:12255 On 22.02.2012 22:39, Tom Anderson wrote: > Evening all, > > I would quite like to represent some numbers in fixed point, and do > arithmetic with them. Fixed point math is susceptible to precision issues which can be more severe than those of float and double: 0.01 * 0.2 -> 0.00 http://en.wikipedia.org/wiki/Fixed_point_arithmetic#Precision_loss_and_overflow Out of curiosity: What do you want to do with that? > 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. What about BigDecimal? http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html You could either add the precision loss after operations or just apply it for output. package math; import java.math.BigDecimal; import java.math.RoundingMode; public class FixedPointMath { public static void main(String[] args) { BigDecimal bd1 = new BigDecimal("0.01"); BigDecimal bd2 = new BigDecimal("0.2"); BigDecimal bd3 = bd1.multiply(bd2); BigDecimal bd4 = bd3.setScale(2, RoundingMode.HALF_UP); System.out.println(bd1 + " * " + bd2 + " -> " + bd3 + " -> " + bd4); } } Alternatively you could use BigInteger and add the scaling logic yourself when cooking your fixed math lib. http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/