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

A few printf '%a' format head-scratchers

From:
Glenn Golden
Date:
November 28, 2015 19:57
Subject:
A few printf '%a' format head-scratchers
Message ID:
20151128195704.GA10074@huh.zplane.com
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

    +1.000000
    +0.000000
    -0.000000
    -1.000000

    +0x10000000000000p+0
    +0x0p+0
    +0x0p+0
    -0x10000000000000p+0


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

    +1.000000
    +0.000000
    -0.000000
    -1.000000

    +0x1.0000000000000p+0
    +0x0.0000000000000p+0
    -0x0.0000000000000p+0
    -0x1.0000000000000p+0

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.



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About