Re: converting exact numbers to binary fixed-point representation

*To*: mathgroup at smc.vnet.net*Subject*: [mg58460] Re: [mg58430] converting exact numbers to binary fixed-point representation*From*: Bob Hanlon <hanlonr at cox.net>*Date*: Sun, 3 Jul 2005 03:57:14 -0400 (EDT)*Reply-to*: hanlonr at cox.net*Sender*: owner-wri-mathgroup at wolfram.com

Look at the functions RealDigits and FromDigits (x=Sin[2*Pi*131/8191])//N//InputForm 0.10031897494050919 d=RealDigits[x,10,17] {{1,0,0,3,1,8,9,7,4,9,4,0,5,0,9,1,9},0} FromDigits[d,10]//N//InputForm 0.1003189749405092 b=RealDigits[x,2,16] {{1,1,0,0,1,1,0,1,0,1,1,1,0,1,0,0},-3} FromDigits[b,2]//N//InputForm 0.10031890869140625 Bob Hanlon > > From: Torsten Coym <torsten.coym at eas.iis.fraunhofer.de> To: mathgroup at smc.vnet.net > Date: 2005/07/02 Sat AM 04:06:34 EDT > Subject: [mg58460] [mg58430] converting exact numbers to binary fixed-point representation > > Hi group, > > > what I want to achieve is to represent the exact value of an irrational > number, say Sin[2*Pi*131/8191], as a binary fixed-point number having 16 > fractional (plus one sign bit) bits. > > First, I thought of converting to floating-point value and then > converting to fixed-point using: > > > Floor[N[Sin[2*Pi*(131/8191)]]*2^16] > > Now I'm worried about the precision of this conversion. The piece of > code above truncates all fractional bits that occur after the left shift > operation. The following two intermediate results (I changed to 4 bits > for simplicity here) 1101,000...1 and 1100,111...1 will end up in two > different code words 1101 and 1100, respectively. > Though both values might be equally close to the exact value, the second > would give the wrong solution. So how can I ensure, that *rounding* the > exact value to a floating-point number will never lead to such a case, > that eventually spoils my 16 bit representation? > > Is there a standard way to solve this problem? > Is this a problem at all or am I worried too much? > > Any explanation is welcome. > > Torsten > >