Additional *float-format* options

I’m still sitting on the third rework of the Nyquist/XLISP reference and have found that there are more float-format options available than described in the old versions of the manuals.

Old - float formats already known:

"%e" - print with exponent
"%f" - print a flat float
"%g" - print the shorter version of the both above[/list]

New - additional flags and options:

+ - always print the sign
# - always print the dot, do not remove zeros after the dot
.n - number of digits after the dot

Examples

Always print the sign:

(setq *float-format* "%+g")
(print  1.1)                     => +1.1
(print -1.1)                     => -1.1

Print floats with ten digits after the dot:

(setq *float-format* "%#.10g")
(print 1.0)                      =>  1.000000000
(print 1.0e7)                    =>  10000000.00
(print -999.9999999e99)          => -9.999999999e+101

Print floats with ten digits after the dot plus sign:

(setq *float-format* "%+#.10g")
(print  1.2345)                  => +1.234500000
(print -1.2345)                  => -1.234500000

The # is not only needed with “%g”, because “%e” and “%f” always print the dot by default.

The “new” options currently are only tested with Audacity_1.3.13 on my old Debian Linux, but they are built-ins since a long time, they only were nowhere documented, so they should work with all Audacity versions, including Audacity_1.2.x.

Could anybody test this on other Linux Systems or Windows or Mac etc. please?

Thanks,

  • edgar

Nice find Edgar.
I’ve seen the “.n” somewhere before (and used it myself) but the “#” and “+” option are new to me.
Tested on Ubuntu 10.10 with Audacity 1.3.12 and 1.3.13
Tested on Windows with Audacity 1.2.6 , 1.3.7 and 1.3.12

All work as expected.
Test script

(setq x 12.0)
(print (strcat
(progn (setq *float-format* "%e") (format nil "~a~%" x))
(progn (setq *float-format* "%f") (format nil "~a~%" x))
(progn (setq *float-format* "%g") (format nil "~a~%" x))
(progn (setq *float-format* "%+g") (format nil "~a~%" x))
(progn (setq *float-format* "%#g") (format nil "~a~%" x))
(progn (setq *float-format* "%.3f") (format nil "~a~%" x))
))

Output:

1.200000e+01
12.000000
12
+12
12.0000
12.000

Tested Steve’s script on Mac 1.2.6, 1.3.7.1, 1.3.12 and 1.3.13 and all worked as exepcted.

– Bill

Thanks to all for testing!

There’s another one, “Space”, printing a space-character instead of a plus sign:

(setq *float-format* "%g")   ; default format
(print  1.1)                 => 1.1
(print -1.1)                 => -1.1

(setq *float-format* "% g")  ; print a space instead of the + sign
(print  1.1)                 =>  1.1
(print -1.1)                 => -1.1

If you want to participate in this game, the XLISP float-format is the “format” string argument to the underlying “sprintf” C-function. Google for “ANSI C printf format” to get all options for the format string. There are three “printf” functions, “printf” prints to standard out, “fprintf” prints to a file, and “sprintf” prints into a new string, they all three use the same “format” string options. There are many other “printf” options, but only a few make sense with floating-point numbers.

The XLISP integer-format uses the same “sprintf” C-function (same game with “sprintf” format options for integers), maybe there are also more useful options for printing integers with XLISP.

I’m pretty sure that at least the ANSI C “sprintf” options today work on all systems (Windows, Mac, Linux, etc.), but testing is better…

Thanks,

  • edgar