<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META content="MSHTML 6.00.6000.16939" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff background=""><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></BODY></HTML>