[Tutor] converting decimals to fractions

alan.gauld@bt.com alan.gauld@bt.com
Wed, 10 Oct 2001 10:22:40 +0100


Hi Blake,

Two points to make:

1) Your method will work.

2) Its not the most efficient way to use a dictionary.

Given (2), another approach which might(I haven't tested 
this!) be more efficient is to extract the fractional part,
round to the required precision - looks like 6 digits then
do a binary chop search on a list of tuples. The 
indexing/sequential access of the list might in this 
case be better than searching the values of a dictionary.
(Wth 64 entries your *worst* case should be 6 tests...)

The tuples would of course contain the fraction string 
and the equivalent number equivalent. As ever with floats 
beware of rounding errors.

Just some thoughts.

Alan G.