Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: printf



Peter Stephenson wrote:
> 
> Oliver Kiddle wrote:
> > I'd like to be able to support the ' (thousand grouping)
> 
> The more I think about this, the more it seems to me that using quotes is
> simply going to cause grief (unless I've misunderstood).  Is there any
> reason for this rather than the perl version, `_', which isn't going to
> conflict with anything?

Ksh and many newer printf(3)s use the single quote but I agree with you
that it is a bad choice.

Is this `_' a new perl feature:
    perl -e 'printf "%_g\n",132643.45'
does not work with 5.0005_03 or am I putting the underscore in the
wrong place. We can always offer a choice of flag character here.

Going back to my questions earlier this week, fcvt() etc are a bad
idea. They don't exist everywhere, on some (BSD) systems, they are in
compat libraries and sprintf is seemingly just as good but more
available.

My idea of writing a function from scratch to format floats was also a
bad idea. I've been referred to http://plan9.bell-labs.com/netlib/fp as
example source. A brief glance was enough to appreciate that it would
be bloat and overkill as well as being not at all easy.

This returns me to using sprintf which should be okay.

The first issue is dealing with very large precisions specified for
floating point variables. To use sprintf, I would potentially have to
allocate a stupidly large amount of memory which I'd prefer to avoid.

Large widths are not an issue - I'll not use sprintf for the widths but
will just call putchar in a loop before or after outputting the
formatted argument. They might cause problems with print -s and -z
though. I could maybe truncate these to the maximum length a command
can be (how do I find that out again?).

One solution for dealing with large precisions would be to put a limit
on the precision. I can't really see why anyone would want something
like '%.10000f' anyway. An easy solution would be to allocate a buffer
for sprintf which is based on the maximum length of any argument and at
least say 100. You don't get any greater precision than about 60 with
doubles anyway. If someone tries '%.101g', they would then get a
warning that such precisions are not supported. Or I could just
truncate the number returned. Any views on these ideas?

To support the ' flag, I'll try a configure test to check for support
in printf(3) and otherwise, insert the separators retrospectively.

The second issue is working out what is going on with locales. Does
basic printf do locales on any platforms? The I flag is glibc 2.2. I'd
like to have the I flag in zsh and locales no used without it so that
users can rely on the output of printf. Unfortunately, every machine I
have access to seems to only have English locale data installed so it
is hard to work out what is happening.

Oliver

_____________________________________________________________________
This message has been checked for all known viruses by the 
MessageLabs Virus Scanning Service. For further information visit
http://www.messagelabs.com/stats.asp



Messages sorted by: Reverse Date, Date, Thread, Author