Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!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: Mon, 27 Feb 2012 07:32:26 +0100 Lines: 44 Message-ID: <9r0mcoFjjtU1@mid.individual.net> References: <9ql7jpF3mnU1@mid.individual.net> <4f4ae165$0$294$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: individual.net KWo5invJVljHa8w2JVHxRAl/tNyz/9DKoNRWwYQSUSjaX2r7A= Cancel-Lock: sha1:T0mGt+74bN1B7Gtq2QEj4AGSi0o= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 In-Reply-To: <4f4ae165$0$294$14726298@news.sunsite.dk> Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:12424 On 27.02.2012 02:50, Arne Vajh=F8j wrote: > On 2/26/2012 7:59 PM, glen herrmannsfeldt wrote: >> Robert Klemme wrote: >> >> (snip) >>>> 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 >> >> In PL/I, if you multiply, you get the appropriate number if digits >> after the radix point. >> >> FIXED DECIMAL(3,2) times FIXED DECIMAL(2,1) generates a >> product of FIXED DECIMAL(5,3). If you force it to throw away >> low order digits, then, yes you can lose precision. > > That is rather similar to how BigDecimal works. And that is not exactly how FP arithmetic is described in Wikipedia: "For simplicity, fixed-point multiply procedures use the same result=20 format as the operands. This has the effect of keeping the middle bits;=20 the I-number of least significant integer bits, and the Q-number of most = significant fractional bits. Fractional bits lost below this value=20 represent a precision loss which is common in fractional multiplication. = If any integer bits are lost, however, the value will be radically=20 inaccurate." It boils down to how fixed "fixed" is. In my understanding decimal=20 places are fixed throughout the whole calculation which is not what BD=20 does (obviously to avoid precision loss). Kind regards robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/