My question is:
How can I set the precision of my REPL print output?
As an example, this simple function here:
(defun gaussian (rows cols sigma)
(let ((filter (make-array `(,rows ,cols)))
(rowOffset (/ (- rows 1) 2.0))
(colOffset (/ (- cols 1) 2.0)))
(loop for i from 0 to (- rows 1)
do (loop for j from 0 to (- cols 1)
do (setf (aref filter i j)
(gaussDistVal i j rowOffset ColOffset sigma))))
filter))
If I call (gaussian 5 5 1)
, my output is the following:
#2A((0.01831564 0.082085 0.13533528 0.082085 0.01831564)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.13533528 0.60653067 1.0 0.60653067 0.13533528)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.01831564 0.082085 0.13533528 0.082085 0.01831564))
Whereas I'd like to get:
#2A((0.0 0.1 0.1 0.1 0.0)
(0.0 0.4 0.6 0.4 0.1)
(0.1 0.6 1.0 0.6 0.1)
(0.0 0.4 0.6 0.4 0.1)
(0.0 0.1 0.1 0.1 0.0))
If you have the answer, could you also please tell me where these "REPL customisations" are documented?
(SBCL 1.2.11; Slime on Emacs 25)
Using the Common Lisp pretty printer
Common Lisp has an extensive pretty printer. A rarely used feature is a dispatch table for controlling the printing of objects of a certain type. See set-pprint-dispatch how to configure this functionality.
The function format
has features to output various forms of float numbers.
This example combines both:
CL-USER 32 > (set-pprint-dispatch 'float
#'(lambda (s obj)
(format s "~,1F" obj)))
NIL
CL-USER 33 > (setf *print-pretty* t)
T
CL-USER 34 > #2A((0.01831564 0.082085 0.13533528 0.082085 0.01831564)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.13533528 0.60653067 1.0 0.60653067 0.13533528)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.01831564 0.082085 0.13533528 0.082085 0.01831564))
#2A((0.0 0.1 0.1 0.1 0.0)
(0.1 0.4 0.6 0.4 0.1)
(0.1 0.6 1.0 0.6 0.1)
(0.1 0.4 0.6 0.4 0.1)
(0.0 0.1 0.1 0.1 0.0))
One may also want to use it temporarily:
CL-USER 37 > (let ((*print-pprint-dispatch* (copy-pprint-dispatch)))
(set-pprint-dispatch 'float
#'(lambda (s obj)
(format s "~,1F" obj)))
(pprint #2A((0.01831564 0.082085 0.13533528 0.082085 0.01831564)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.13533528 0.60653067 1.0 0.60653067 0.13533528)
(0.082085 0.36787945 0.60653067 0.36787945 0.082085)
(0.01831564 0.082085 0.13533528 0.082085 0.01831564))))
#2A((0.0 0.1 0.1 0.1 0.0)
(0.1 0.4 0.6 0.4 0.1)
(0.1 0.6 1.0 0.6 0.1)
(0.1 0.4 0.6 0.4 0.1)
(0.0 0.1 0.1 0.1 0.0))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With