> scheme48 (and other schemes in general) seem to go out of their way to
> convert things to rationals, which is a great way to avoid loss of
> precision, but sometimes you really want to see 4/3 as 1.33333 ...
>
> What's the "right" way to do this in scheme?
I agree it's a problem. I don't know that there's a "right" way of
doing this. However, in a Scheme that gave you the ability to alter
the top-level REP handler, you could just install a new handler that
converts to scientific notation. For instance, in Chez Scheme, I
would write
(let ((ce (current-eval)))
(current-eval
(lambda (x)
(call-with-values
(lambda () (ce x))
(lambda vals
(apply values
(map (lambda (n)
(if (and (rational? n) (not (integer? n)))
(exact->inexact n)
n))
vals)))))))
current-eval is a system parameter that returns the current evaluator
(when invoked with no arguments) and installs a new one (when invoked
with one). exact->inexact will usually give you what you want. This
code doesn't do the right thing for complex numbers, but it should be
pretty easy to extend.
The advantage is that all internal computations are still done on
rationals (wherever possible), so you don't lose accuracy until the
final step.
'shriram
|