<div dir="ltr">If the input numbers are constrained to be rationals, shouldn't it be enough to check for the first repeated digit?<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Jan 16, 2014 at 4:11 AM, Marc Feeley <span dir="ltr"><<a href="mailto:feeley@iro.umontreal.ca" target="_blank">feeley@iro.umontreal.ca</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
On Jan 15, 2014, at 8:12 PM, Mikael <<a href="mailto:mikael.rcv@gmail.com">mikael.rcv@gmail.com</a>> wrote:<br>
<br>
><br>
><br>
> 2014/1/15 Marc Feeley <<a href="mailto:feeley@iro.umontreal.ca">feeley@iro.umontreal.ca</a>><br>
><br>
> On Jan 15, 2014, at 5:04 PM, Mikael <<a href="mailto:mikael.rcv@gmail.com">mikael.rcv@gmail.com</a>> wrote:<br>
><br>
> > 2014/1/15 Mikael <<a href="mailto:mikael.rcv@gmail.com">mikael.rcv@gmail.com</a>><br>
> > 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.<br>

> ><br>
> > Yeah if anyone has an example implementation of such an algorithm |has-infinite-decimals?| feel free to post :)<br>
><br>
> It is really very simple.  I’ll let you think about it some more before posting a solution…<br>
><br>
> Marc<br>
><br>
><br>
> I trust it's both simple and elegant - implementing something and do not have the proper focus on math in this moment;<br>
><br>
> It just struck me that it's an awesome symmetry that there's this full support both re input, handling and output (per algorithm I suggested in previous email, guess that's how fast at is goes) of these, and the dot on the i in this respect would be the ability to check that a number has a finite number of decimals - saw an algorithm but didn't take it to the level of implementing it - , that was all I was thinking today.<br>

><br>
> Mikael<br>
<br>
</div></div>The following algorithm is not optimal, but it has the virtue of being simple.  It puts the repeating decimals in […] if there are any (in other words 1/3 prints as 0.[3] meaning 0.33333333...).<br>
<br>
Marc<br>
<br>
<br>
(define (println-decimal n) ;; n is a positive real<br>
<br>
  (define (generator frac states digits)<br>
    (if (= frac 0)<br>
        (reverse digits)<br>
        (let ((seen (member frac states)))<br>
          (if seen<br>
              (let* ((i (- (length seen) 1))<br>
                     (len (length states)))<br>
                (append (reverse (list-tail digits (- len i)))<br>
                        '(#\[)<br>
                        (list-tail (reverse digits) i)<br>
                        '(#\])))<br>
              (let* ((frac*10 (* 10 frac))<br>
                     (digit (truncate frac*10)))<br>
                (generator (- frac*10 digit)<br>
                           (cons frac states)<br>
                           (cons digit digits)))))))<br>
<br>
  (println<br>
   (let* ((exact (inexact->exact n))<br>
          (int (truncate exact)))<br>
     (list int #\. (generator (- exact int) '() '())))))<br>
<br>
(println-decimal 1/1024)<br>
(println-decimal 1/999)<br>
(println-decimal 5/700)<br>
(println-decimal 0.1)<br>
(println-decimal 0.2)<br>
(println-decimal 0.3)<br>
(println-decimal (* 4 (atan 1)))<br>
(println-decimal (expt 1/3 7))<br>
<br>
;; output:<br>
;;<br>
;; 0.0009765625<br>
;; 0.[001]<br>
;; 0.00[714285]<br>
;; 0.1000000000000000055511151231257827021181583404541015625<br>
;; 0.200000000000000011102230246251565404236316680908203125<br>
;; 0.299999999999999988897769753748434595763683319091796875<br>
;; 3.141592653589793115997963468544185161590576171875<br>
;; 0.[000457247370827617741197988111568358481938728852309099222679469593049839963420210333790580704160951074531321444901691815272062185642432556012802926383173296753543667123914037494284407864654778235025148605395518975765889346136259716506630086877]<br>

<div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
Gambit-list mailing list<br>
<a href="mailto:Gambit-list@iro.umontreal.ca">Gambit-list@iro.umontreal.ca</a><br>
<a href="https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list" target="_blank">https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list</a><br>
</div></div></blockquote></div><br></div>