Would it be possible to use the log method as a first approximation, take the number that is 1 or 2 orders lower, then use the iteration method on only the last few iterations?<br><br>Laurent<br><br><div class="gmail_quote">
2009/11/5 Jos Koot <span dir="ltr"><<a href="mailto:jos.koot@telefonica.net">jos.koot@telefonica.net</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div bgcolor="#ffffff">
<div><font face="Courier New" size="2">Hi Phil,</font></div>
<div><font face="Courier New" size="2"></font> </div>
<div><font face="Courier New" size="2">Yes, that is an exact method (provided b is
exact) and the code is straight forward indeed. </font><font face="Courier New" size="2">As you say, it takes a while. That's why I try to use primitive log.
</font><font face="Courier New" size="2">In fact I use a method that computes the
logarithms (base 10) of the numerator and nominator of rationals separately such
as to avoid -inf.0 for (positive) rational numbers very close to zero (not shown
in my example)</font></div>
<div><font face="Courier New" size="2"></font> </div>
<div><font size="2"><font face="Courier New">We have something like <span title="Provided from: scheme/base, scheme"><span><a>integer-sqrt/remainder</a> for
square roots. It would be nice to have something like
(integer-log-base-10/remainder n) --> p r </span></span></font></font><font size="2"><font face="Courier New"><span title="Provided from: scheme/base, scheme"><span>such that (+ (expt
10 p) r) exactly equals n, where n, p and r are positive exact integer numbers
and p is computed to be maximal.</span></span></font></font></div>
<div><font face="Courier New" size="2"></font> </div>
<div><font face="Courier New" size="2">Jos</font></div>
<blockquote style="border-left: 2px solid rgb(0, 0, 0); padding-right: 0px; padding-left: 5px; margin-left: 5px; margin-right: 0px;"><div class="im">
<div style="font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">----- Original Message ----- </div>
<div style="background: rgb(228, 228, 228) none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous; font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">
<b>From:</b>
<a title="pbewig@gmail.com" href="mailto:pbewig@gmail.com" target="_blank">Phil Bewig</a> </div>
<div style="font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><b>To:</b> <a title="jos.koot@telefonica.net" href="mailto:jos.koot@telefonica.net" target="_blank">Jos Koot</a> </div>
<div style="font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><b>Cc:</b> <a title="plt-scheme@list.cs.brown.edu" href="mailto:plt-scheme@list.cs.brown.edu" target="_blank">plt-scheme@list.cs.brown.edu</a>
</div>
</div><div class="im"><div style="font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><b>Sent:</b> Thursday, November 05, 2009 4:02
PM</div>
<div style="font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; font-size: 10pt; line-height: normal; font-size-adjust: none; font-stretch: normal;"><b>Subject:</b> Re: [plt-scheme] order of
magnitude</div>
<div><br></div></div><code style="font-family: courier new,monospace;">(define (ilog
b n)<br>Â (let loop ((n n) (i -1))<br>Â Â Â (if (zero?
n) i<br>Â Â Â Â Â (loop (quotient n b) (+ i
1)))))<br><br>(ilog 10 #e1000e1000000) => 1000003<br></code><br style="font-family: courier new,monospace;">
<div class="gmail_quote"><div class="im"><span style="font-family: courier new,monospace;">It
does take a while....</span><br><br>On Thu, Nov 5, 2009 at 5:04 AM, Jos Koot
<span dir="ltr"><<a href="mailto:jos.koot@telefonica.net" target="_blank">jos.koot@telefonica.net</a>></span> wrote:<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div bgcolor="#ffffff"><font face="Courier New" size="2"><div class="im">Some browsing did not
lead me to finding a function that accepts an exact positive rational number
and returns its order of magnitude (an exact integer number) Something
like:<br><br>(define log10 (inexact->exact (log 10)))<br>(define
(order-of-magnitude q) (floor (/ (inexact->exact (log q))
log10)))<br><br>However, due to inexactness of function log we
find:<br><br>(/ (inexact->exact (log #e1000e1000000)) log10) --> close
to but sligtly less than 1000003<br><br>and
hence:<br><br>(order-of-magnitude #e1000e1000000) ; --> 1000002
<br><br>The wanted answer is 1000003. So I did the following:<br><br>(define
order-of-magnitude<br></div>Â (let*<br>Â ((log10 (inexact->exact
(log 10)))<br>  (10log (λ (q) (/ (inexact->exact (log q))
log10))))<br> (λ (q)<br>  (unless (and (rational? q)
(exact? q) (positive? q))<br>Â Â Â (raise-type-error
'magnitude "positive exact rational number" q))<br>Â Â (let* ((m
(floor (10log q))) (k (expt 10 m)))<br>Â Â Â ; >From now on
all arithmetic operations and their operands are
exact.<br>Â Â Â (let loop ((m m) (lower (* k 1/10)) (middle
k) (upper (* k 10)))<br>Â Â Â Â
(cond<br>Â Â Â Â Â ((<= q lower) (loop (sub1 m)
(* lower 1/10) lower middle))<br>Â Â Â Â Â ((>=
q upper) (loop (add1 m) middle upper (* upper
10)))<br>Â Â Â Â Â (else
m)))))))<div class="im"><br><br>(order-of-magnitude #e1000e1000000) ; -->
1000003<br><br>However, this seems rather complicated. Does someone have or
know of a simpler and faster function for this purpose?<br><br>Thanks,
Jos</div></font></div><br>_________________________________________________<br>Â For
list-related administrative tasks:<br>Â <a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme" target="_blank">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br><br></blockquote></div><br></blockquote>
</div>
<br>_________________________________________________<br>
For list-related administrative tasks:<br>
<a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme" target="_blank">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a><br>
<br></blockquote></div><br>