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

Mikael mikael.rcv at gmail.com
Wed Jan 15 14:47:41 EST 2014


After thinking this through a bit, I guess the algorithm I suggested is as
fast as it goes, as because it's a rational, the actual result decimal
digits are *not* available plaintext in the number's internal structures or
alike, so this is the fastest algorithm. If you have any other thought on
this feel free to tell.

I believe you addressed now how to detect whether a rational has inifinite
decimals - yes I got aware there's an algorithm to do this too, (even while
it's iterative - would be nice to understand how fast it is, anyhow) neat.

I agree with you fully that being exposed to challenging questions at every
age does good to the soul.

Mikael





2014/1/15 Bradley Lucier <lucier at math.purdue.edu>

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20140115/214ef910/attachment.htm>


More information about the Gambit-list mailing list