Prev: A portable LISP interpreter that includes all the major list-processing functions is described. A complete, annotated listing of the program's code, written in PASCAL, is included.
Next: accumulate function in Lisp
From: Frank Buss on 24 Jul 2010 06:55 I guess it is somewhere in the CLHS, but I can't find it. How can I print a floating point number as the C printf function does it? I want always 1.0E-5, regardless of the number type of the argument: CL-USER > (format t "~g" 1e-5) 1.0E-5 NIL CL-USER > (format t "~g" 1d-5) 1.0D-5 NIL CL-USER > (format t "~g" 1s-5) 1.0S-5 -- Frank Buss, fb(a)frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
From: francogrex on 24 Jul 2010 07:39 In article <g86fg5qrav5y.812zn5kzt77z$.dlg(a)40tude.net>, fb(a)frank-buss.de says .... >I guess it is somewhere in the CLHS, but I can't find it. How can I print a >floating point number as the C printf function does it? I want always >1.0E-5, regardless of the number type of the argument: > >CL-USER > (format t "~g" 1e-5) >1.0E-5 >NIL > >CL-USER > (format t "~g" 1d-5) >1.0D-5 >NIL > >CL-USER > (format t "~g" 1s-5) >1.0S-5 I'm sure there is a more elegant way but this does it at least for this specific example you gave. > (format t "~1,1,1,0,,,'eE" 1e-5) 1.0e-5 NIL > (format t "~1,1,1,1,,,'eE" 1d-5) 1.0e-5 NIL > (format t "~1,1,1,0,,,'eE" 1s-5) 1.0e-5 NIL >
From: Frank Buss on 24 Jul 2010 08:03 francogrex wrote: > I'm sure there is a more elegant way but this does it at least for this > specific example you gave. > >> (format t "~1,1,1,0,,,'eE" 1e-5) > 1.0e-5 > NIL >> (format t "~1,1,1,1,,,'eE" 1d-5) > 1.0e-5 > NIL >> (format t "~1,1,1,0,,,'eE" 1s-5) > 1.0e-5 > NIL >> This doesn't work for higher precision, but thanks for the hint, looks like this one works for me: (format t "~,,,,,,'ee" foo) -- Frank Buss, fb(a)frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
From: Thomas A. Russ on 26 Jul 2010 00:07
Frank Buss <fb(a)frank-buss.de> writes: > I guess it is somewhere in the CLHS, but I can't find it. How can I print a > floating point number as the C printf function does it? As an aside, it is probably easier to get CL to format floats the way FORTRAN does it.... > I want always > 1.0E-5, regardless of the number type of the argument: > > CL-USER > (format t "~g" 1e-5) > 1.0E-5 > NIL > > CL-USER > (format t "~g" 1d-5) > 1.0D-5 > NIL > > CL-USER > (format t "~g" 1s-5) > 1.0S-5 Well, for starters, let me say that I like Francogrex' solution the best, since it uses precisely the correct format directive for specifying the exponent character explicitly. The reason that you get different exponent characters is because CL is trying to preserve reading as the correct floating point type. You can control which floating point type "e" maps to by setting the variable *READ-DEFAULT-FLOAT-FORMAT* to be of the type you want. So for example, you could do (defun format-with-e (number) (assert (typep number 'number)) (setq number (coerce number 'float)) (let ((*read-default-float-format* (type-of number))) (format t "~e" number))) -- Thomas A. Russ, USC/Information Sciences Institute |