jholt has asked for the wisdom of the Perl Monks concerning the following question:

Here are two seemingly contradictory quotes from the perl sprintf documentation:

quote #1

Perl does its own sprintf formatting--it emulates the C function sprintf, but it doesn't use it (except for floating-point numbers, and even then only the standard modifiers are allowed). As a result, any non-standard extensions in your local sprintf are not available from Perl.

quote #2

Note that the number of exponent digits in the scientific notation produced by %e , %E , %g and %G for numbers with the modulus of the exponent less than 100 is system-dependent: it may be three or less (zero-padded as necessary). In other words, 1.23 times ten to the 99th may be either "1.23e99" or "1.23e099".

So, if perl uses its own, then why can't it behave the same way on all systems?

In my perlbug report (#48881) I expressed my, uh, dissatisfaction.

I just reread the first quote and for some reason I just got glossy-eyed when I encountered the exception. I suppose for performance reasons, perl uses native libraries for some (all?) floating point operations?

Replies are listed 'Best First'.
Re: sprintf and scientific notation
by ikegami (Patriarch) on Dec 19, 2007 at 18:31 UTC

    It could be that Perl parses the line format itself (to convert scalars to the appropriate type, to perform checks on the number of arguments, etc), but delegates the actual formating to the C library.

    But I agree with you. Perl shouldn't differ between systems when it doesn't have to. Its handling of Infinity is another example where the availability and implementation of an element of the language is dependent on the system.

Re: sprintf and scientific notation
by aquarium (Curate) on Dec 20, 2007 at 00:50 UTC
    I don't see any contradictions or other problems with these two statements/quotes. In my understanding of those statements, perl calls C/system sprintf rarely, further munging the result as it feels fit.
    In regards to portability, most languages try to be as portable as possible but are never 100% portable without defensive coding sensitive to the issues. Even such a long ago developed language as C (ANSI C even) has plenty of differences between platforms.
    the hardest line to type correctly is: stty erase ^H