From:
                                                           10/13/97 11:01 PM
Subject: MAX Digest - 12 Oct 1997 to 13 Oct
1997To: Recipients of MAX digests 

There is one message totalling 83 lines in this issue.

Topics of the day:

  1. sprintf

----------------------------------------------------------------------

Date:    Mon, 13 Oct 1997 13:04:20 -0700
From:    Peter Elsea 
Subject: sprintf

I've expounded on what I know about sprintf in the article Max&ASSCII,
available for download at ftp://arts.ucsc.edu/pub/ems. Here's an excerpt
that adresses John's question:

The help file and manual are not complete in their description of sprintf,
referring you  to "any standard C language reference", not a common item on
most coffee tables. Here's what the placeholders mean:

%ld or % d      Replace with an int and display as decimal. (Floats are
truncated.)
%x or % X       Replace with an int and display as hexadecimal.
%o      Replace with an int and display as octal. (There's one you don't
see much any more!)
%u      Replace with an int and display unsigned. (In theory, this should
let you display numbers larger than 2874836647, but it doesn't.)
%f      Replace with a float. The format %.2f will round the float to 2
decimal places.
%s      Replace with a symbol. The format %.2s will show the first 2
letters of the symbol.
%c      Replace with character.

There are other options in the C language printf, but they are not really
appropriate to Max and not implemented. (Or partially implemented- try
%05.5X) (A warning - C allows %* in some cases- that one will bomb.)

Here's a more complete description, cribbed from Harbison and Steele:
printf conversion specifications are  formated %[flag][field
width][precision][long size][conversion]
everything but the % and the conversion are optional.
Flags may be:
- left justify (Max left justifies anyway. Works on ints, suppressing
leading 0s)
0 use leading 0s
+ always show sign
[space] use leading spaces (won't work in Max)
# modifies some conversion operations (won't work in Max)

field width is the minimum number of places used. This should be padded
with spaces or 0s. The decimal or sign takes one of those places. In Max
this works only if leading 0  is specified and there are other characters
preceeding or following the number. (Option space will give you the look of
raw numbers) If there's no pad specified and the converted characters are
preceeded by something else, you may get a single space.  You can't specify
spaces as pad, because the parser uses spaces to seperate arguments and
generates error messages.

Precison is indicated with a dot, as .3. Precision rounds off floats. This
works right if the number is preceeded or followed by some other character
in the sprintf string- with the string %.2f", 1.234 is printed as 1.23, but
if there is nothing (or another convertable argument) after the f, you get
1.230000.   A # is supposed to show a decimal point when the precison is 0,
but this doesn't work. An * is supposed to give you a variable precison,
but that will bomb. With ints, precision will give you leading 0s if
necessary.

Long size is indicated with an l. This is important with big numbers
(>32767), because only 16 bits are converted when you leave it out.

There are many more conversion specifiers:
E or e will give you scientific notation.

You can use g for floats, but the precision is different. Instead of
rounding off to a fixed number of decimal places, you get scientific
precison-  0.0001 rounded off to 2 places is 0.00, with a precison of 2
it's 0.0001.

u is badly explained above (I'll fix that next edition). You get the true
value for negative numbers; -1 shows up as 65535 with u and 4294967295 with
lu. This can only be seen if the numbers are within a string. By themselves
you get 0 or 2147483747 for any negative number.

Peter Elsea
Electronic Music Studios
University of California, Santa Cruz
http://arts.ucsc.edu/EMS/Music/index.html
 elsea@cats.ucsc.edu

------------------------------

End of MAX Digest - 12 Oct 1997 to 13 Oct 1997
**********************************************