Can implicit conversion be controlled by Lisp's FORMAT function?

While trying to round to nine digits, I came across the following:

? (format t "~9$" 763700091/112000148)
6.818742300
Nil

      

But rounding only to 5 digits. I expected to format

take care of this automatically; those. convert a fraction to double, given the number of digits needed to represent it, but format

doesn't seem to work. If you double this fraction, you get:

? (coerce 763700091/112000148 'double-float)
6.818741802019762D0

      

Is there a control directive to instruct to format

use double precision in this case. Or do I have to force the fraction to double-float

before transferring it to the format to accomplish this?

I know this format

is terribly complex and you can certainly do it by calling an external function, but I hope to get away with a simpler setup~9$

+3


source to share


2 answers


The format string documentation states:

If arg is a rational number, then it is forced to be one float and then printed.



So the answer to your question is no , but you can easily combine everything you did to round up the number you just attached to the double float:

(format nil "~9$" (coerce 763700091/112000148 'double-float))
; ==> "6.818741802"

      

+8


source


It's impossible. From CLHS 22.3.3.4 Tilde Dollarsign: Monetary Floating Point

If arg is a rational number then it is forced to be the only float and then printed.



No provision has been made to indicate an alternative type for enforcement.

+4


source







All Articles