Subject: [clisp-list] FORMAT ~G problem

From: [email-masked]
Date: Mon Mar 29 10:45:02 2004

Hello,
I experienced a problem with FORMAT--when printing floats with ~G,
trailing blanks were introduced:

(format t "---|~@G|---~%" (exp 1.0d0))
---|+2.718281828459045    |---
                      ^^^^
                      these blanks should not be there according to
my understanding of the CLHS.  GCL appears to get it right:

$ gcl
GCL (GNU Common Lisp)  Version(2.4.0) Tue May 29 11:41:02 MET DST 2001
Licensed under GNU Library General Public License
Contains Enhancements by W. Schelter

>(format t "---|~@G|---~%" (exp 1.0d0))
---|+2.718281828459046|---
NIL

Since my CLISP was ancient, I built CLISP 2.33 on Solaris:

Platform:
  SunOS rosalind 5.8 Generic_108528-12 sun4u sparc

Source:
  http://heanet.dl.sourceforge.net/sourceforge/clisp/clisp-2.33.tar.bz2
  as of 2004-03-29
  MD5 (clisp-2.33.tar.bz2) = 8724eccb8933eedec31a06206c79e74d

Build:
  export CC="gcc -Wall"
  ./configure --prefix=/home/kau/3/ solaris
  cd solaris
  ./makemake --with-dynamic-ffi  --prefix=/home/kau/3/   > Makefile
  make config.lisp
  emacs config.lisp  # just long-site-name
  time make

  I can send the build log if necessary (there were a number of gcc
  warnings and suspicious messages like
(WARN "~a: redefining ~a ~s in ~a, was defined in ~a" "DEFUN/DEFMACRO"
 "function" FORMAT #P"/home/kau/3/src/clisp-2.33/src/format.lisp"
 "top-level") 

I got a working clisp (I have not ran the tests yet):
./clisp --version
GNU CLISP 2.33 (2004-03-17) (built 3289563544) (memory 3289564749)
Software: GNU C 3.1 ANSI C program
Features: 
(CLOS LOOP COMPILER CLISP ANSI-CL COMMON-LISP LISP=CL INTERPRETER SOCKETS
 GENERIC-STREAMS LOGICAL-PATHNAMES SCREEN FFI GETTEXT UNICODE
 BASE-CHAR=CHARACTER UNIX)
Installation directory: /home/kau/3/src/clisp-2.33/solaris/
User language: ENGLISH
Machine: SUN4U (SUN4U) rosalind [172.27.96.18]

but FORMAT behaves as before.

I believe that the problem lies in src/format.lisp:

;; ~G, CLTL p.395-396, CLtL2 p. 594-595
(defformat-simple format-general-float (stream colon-modifier atsign-modifier
                  (w nil) (d nil) (e nil) (k 1)
                  (overflowchar nil) (padchar #\Space) (exponentchar nil))
                  (arg)
  (declare (ignore colon-modifier))
  (if (rationalp arg) (setq arg (float arg)))
  (if (floatp arg)
    (multiple-value-bind (mantissa n) (format-scale-exponent (abs arg))
      (declare (ignore mantissa))
      (if (null d)
        (setq d
          (multiple-value-bind (digits digitslength)
            (format-float-to-string (abs arg) nil nil nil nil)
            (declare (ignore digits))
            (max (max (1- digitslength) 1) (min n 7)))))
      (let* ((ee (if e (+ 2 e) 4))
             (dd (- d n)))
        (if (<= 0 dd d)
          (progn
            (format-float-for-f
              (if w (- w ee) nil)
              dd 0
              overflowchar padchar atsign-modifier arg stream)
;;; Is this the cause of the problem?
            ;;(format-padding ee #\Space stream)
            )
          (format-float-for-e w d e k overflowchar padchar exponentchar
                              atsign-modifier arg stream))))
    (if w
      (format-padded-string w 1 0 padchar t (princ-to-string arg) stream)
      (format-ascii-decimal arg stream))))

Loading the modified form into CLISP establishes the correct
behaviour:

[5]> (load "src/format-g-loader.lisp")
;; Loading file src/format-g-loader.lisp ...
;;  Loading file src/format-g.lisp ...
;;  Loaded file src/format-g.lisp
;; Loaded file src/format-g-loader.lisp
T
[6]> (format t "|~G|" pi)
|3.1415926535897932385|
NIL
[7]> (format t "|~10,,,,G|" pi)
|3.1415926535897932385|
NIL
[8]> (format t "|~10,5,,,G|" pi)
|3.1416|
NIL
[9]> (format t "|~20,5,,,G|" pi)
|          3.1416|
NIL

Could some of the experts here comment on the problem and my
workaround?  Please CC me, as I have not yet subscribed to the list.
                                    regards
                                        Roland

---

From: Sam Steingold <[email-masked]>
Date: Mon Mar 29 12:42:03 2004

> *  <[email-masked]> [2004-03-29 20:44:05 +0200]:
>
that's also what LispWorks does.

> my understanding of the CLHS.

<http://www.lisp.org/HyperSpec/Body/sec_22-3-3-3.html>
Let ee equal e+2, or 4 if e is omitted.
~ww,dd,,overflowchar,padcharF~ee@T

~ee@T is what outputs these 4 spaces.

> GCL appears to get it right:
>
> $ gcl

yes, GCL is known to have many ANSI compliance bugs.

-- 
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.honestreporting.com>
Those who value Life above Freedom are destined to lose both.