[gambit-list] Any rational-number->string with infinite precision for decimal output? (i.e. d.ddddd rather than x/y)

Bradley Lucier lucier at math.purdue.edu
Wed Jan 15 13:42:37 EST 2014


On 01/15/2014 12:58 PM, Mikael wrote:
> Ah, actually this can be implemented for #e12345.6789 by first
> flooring and printing that out ("12345") then deducting that from the
> value (=> 0.6789) and then doing * 10 and |truncate| up to eq? 0 -
> that works. Perhaps it'd even be quite close to optimum speed?
>
> I.e. #e12345.6789 (floor #) (- ## #) , and then repeat (eq? # 0) (* ##
> 10) (truncate #) (- ## #).

Well, this is a mathematical question rather than a programming
question: Are rational numbers and repeating decimals (possibly
repeating 0 at the end) the same kind of numbers?  The answer is yes, of
course.

To go from repeating decimals, where you have a pattern <pattern> of r
repeating digits at the end, multiply by a power of 10 ($10^k$, say)
such that the digits behind the decimal point are the start of the
repeating pattern, then multiply the number again by $10^{r+k}$:

10^{r+k}x = integer<pattern>.<pattern><pattern>...
10^kx     =          integer.<pattern><pattern>...

subtract

(10^{r+k}-10^k)x=(integer<pattern> - integer)

so

x = (integer<pattern> - integer)/(10^{r+k}-10^k)

To go the other way, if $x=p/q$, divide p.00000000... by q in longhand. 
If any of the remainders are 0, then the division ends and 0 is repeated
indefinitely.  Otherwise, there can be only q-1 different remainders, so
eventually one of them will have to repeat; after that the digits in the
quotient repeat indefinitely.

I'm old enough to have taken the "New Math" in the 1960s, and I remember
my teacher in 6th or 7th grade asking this question.  They asked pretty
sophisticated questions in those days, even if most of the students
didn't really realize it.

Brad



More information about the Gambit-list mailing list