Implement the "unum" representation in D ?

Ivan Kazmenko gassa at mail.ru
Fri Feb 21 01:04:38 PST 2014


On Friday, 21 February 2014 at 05:21:53 UTC, Frustrated wrote:
> I think though adding a "repeating" bit would make it even more
> accurate so that repeating decimals within the bounds of maximum
> bits used could be represented perfectly. e.g., 1/3 = 0.3333...
> could be represented perfectly with such a bit and sliding fp
> type. With proper cpu support one could have 0.3333... * 3 = 1
> exactly.

I believe that the repeating decimals, or better, repeating 
binary fractions, will hardly be more useful than a rational 
representation like p/q.  The reason is, if we take a reciprocal 
1/q and represent it as a binary, decimal, or other fixed-base 
fraction, the representation is surely periodic, but the upper 
bound on the length of its period is as high as q-1, and it is 
not unlikely to be the exact bound.

For example, at 
http://en.wikipedia.org/wiki/Binary_number#Fractions , note that 
the binary fraction for 1/11 has period 10, and for 1/13 the 
period is 12.

Thus repeating decimal for a fraction p/q will take up to q-1 
bits when we store it as a repeating decimal, but log(p)+log(q) 
bits when stored as a rational number (numerator and denominator).

Ivan Kazmenko.


More information about the Digitalmars-d mailing list