develooper Front page | perl.perl5.porters | Postings from November 2015

A few printf '%a' format head-scratchers

Glenn Golden
November 28, 2015 19:57
A few printf '%a' format head-scratchers
Message ID:
This script,

    @vals = (+1, +0.0, -0.0, -1);
    foreach (@vals) { printf("%+f\n",         $_); } print "\n";
    foreach (@vals) { printf("%+-.13a\n",     $_); } print "\n";

run under perl 5.22.0 on an i386 produces



This C program,

  int main(int argc, char *argv[])
    double  vals[4] = {+1.0, +0.0, -0.0, -1.0};
    int     i;

    for (i = 0; i < 4; ++i) { printf("%+f\n",      vals[i]); }; printf("\n");
    for (i = 0; i < 4; ++i) { printf("%+-.13a\n",  vals[i]); }; printf("\n");

compiled with gcc 5.2.0 on the same machine produces



The %f results are identical between perl and C. The %a results differ:

 1. The perl script loses the sign of the negative zero.
 2. The perl script does not display the zeros with the requested precision.
 3. The perl script does not display the radix point after the leftmost
    hex digit.

To my inexpert eye, #1 and #2 seem like bugs, but perhaps there's some 
subtlety involving implied type conversion that I'm not appreciating. Maybe
the two zeros are getting (mis?)interpreted as integers...?

#3 may simply be a design choice not to include the radix point, but seemed
worth pointing out anyway.

Please let me know if I should file a perlbug on any/all of the above. Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About