<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-7">
<META content="MSHTML 6.00.6000.16939" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY 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 class=ScmSym><A
class=ScmValLink
href="file:///C:/Program%20Files/PLT-FULL-4.2.2.4/doc/reference/numbers.html#(def._((quote._~23~25kernel)._integer-sqrt/remainder))">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 class=ScmSym>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="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV
style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B>
<A title=pbewig@gmail.com href="mailto:pbewig@gmail.com">Phil Bewig</A> </DIV>
<DIV style="FONT: 10pt arial"><B>To:</B> <A title=jos.koot@telefonica.net
href="mailto:jos.koot@telefonica.net">Jos Koot</A> </DIV>
<DIV style="FONT: 10pt arial"><B>Cc:</B> <A title=plt-scheme@list.cs.brown.edu
href="mailto:plt-scheme@list.cs.brown.edu">plt-scheme@list.cs.brown.edu</A>
</DIV>
<DIV style="FONT: 10pt arial"><B>Sent:</B> Thursday, November 05, 2009 4:02
PM</DIV>
<DIV style="FONT: 10pt arial"><B>Subject:</B> Re: [plt-scheme] order of
magnitude</DIV>
<DIV><BR></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><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>
<BLOCKQUOTE class=gmail_quote
style="PADDING-LEFT: 1ex; MARGIN: 0pt 0pt 0pt 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">
<DIV bgcolor="#ffffff"><FONT face="Courier New" size=2>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>Â (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)))))))<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</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></BODY></HTML>