lispcommon-lispslime

Change display of floating point values in debugger


When I debug in Slime and inspect the value of a floating point variable, I see something like

6.8998337e-4

However, I find that very hard to read and would prefer

0.00068998337

How can I achieve that?


Solution

  • First:

    CL-USER> (format nil "~F" 6.8998337e-4)
    "0.00068998337"
    CL-USER> (format nil "~E" 6.8998337e-4)
    "6.8998337e-4"
    

    In slime/sly, when you Inspect (C-c I) the value 6.8998337e-4, you get:

    #<SINGLE-FLOAT {3A34E00000000019}>
    --------------------
    Scientific: 6.8998337e-4
    Decoded: 1.0 * 0.70654297 * 2^-10
    Digits: 24
    Precision: 24
    

    The "Scientific" value is formatted in swank/slynk in contrib/swank-fancy-inspector.lisp or contrib/slynk-fancy-inspector.lisp :

    (defmethod emacs-inspect ((f float))
      (cond
        ((float-nan-p f)
         ;; try NaN first because the next tests may perform operations
         ;; that are undefined for NaNs.
         (list "Not a Number."))
        ((not (float-infinity-p f))
         (multiple-value-bind (significand exponent sign) (decode-float f)
           (append
        `("Scientific: " ,(format nil "~E" f) (:newline)
                 "Decoded: "
                 (:value ,sign) " * "
                 (:value ,significand) " * "
                 (:value ,(float-radix f)) "^"
                 (:value ,exponent) (:newline))
        (label-value-line "Digits" (float-digits f))
        (label-value-line "Precision" (float-precision f)))))
        ((> f 0)
         (list "Positive infinity."))
        ((< f 0)
         (list "Negative infinity."))))
    

    You could modify emacs-inspect method and change (format nil "~E" f) to (format nil "~F" f), or you could shadow emacs-inspect with an emacs-inspect :around method to modify the behavior to use "~F".