libc6 printf "alternate format" %#.g prints wrong number of digits

Bug #128355 reported by Wirawan Purwanto
2
Affects Status Importance Assigned to Milestone
GLibC
Fix Released
Medium
glibc (Ubuntu)
Fix Released
Undecided
Unassigned

Bug Description

Binary package hint: libc6

I used Ubuntu 7.04 on x86 system (Pentium M), and updated the libc package to version 2.6: (2.6-3ubuntu2). The error described below also appliled to glibc 2.5, IIRC.

I found that the following C program won't print the correct number of digits:

#include <stdio.h>
int main(int argc, char *argv[], char *env[])
{
    printf("%#.4g\n", 912.98);
    return 0;
}

The output should be "913.0", but my Ubuntu box shows "913." (no trailing zero). The alternate format, according to the C standard, should print out the trailing zeros after decimal point.

This error is shameful and severe! I tried on different systems (Fedora 5, Fedora 6, SunOS, CentOS), none of them show this sickness.

Wirawan

Related branches

Revision history for this message
Wirawan Purwanto (wirawan0) wrote :

This, as it turns out, did not happen if we use libc.a in libc6-dev_2.5-0ubuntu14_i386.deb . So what happens? Perhaps the patch was missing or wrong in the newer libc?

Wirawan

Revision history for this message
In , Wirawan Purwanto (wirawan0) wrote :

Please use the following testcase:

#include <stdio.h>
int main(int argc, char *argv[], char *env[])
{
    printf("%#.4g\n", 912.98);
    return 0;
}

This code should print "913.0", but instead it only prints "913." . It lacks the
trailing zeros, which should be there according to the standard C specs.

I believe that this bug is reproducible on the stock version of glibc 2.6 . I
have established the bug by building the 2.6 library on my local machine, then
linking the program above against the newly built libc.a .

Revision history for this message
Wirawan Purwanto (wirawan0) wrote :

I submitted the corresponding bug report in sources.redhat.com, as bug #4858 . I found out that even the stock glibc 2.6 has it.

Wirawan

Revision history for this message
In , Jakub Jelinek (jakub-redhat) wrote :

This is a regression caused by the BZ#4070 fix, particularly the
+ else if (__builtin_expect (spec == 'g' && type == 'f' && info->alt,
+ 0))
+ /* This is a special case: the rounded number is 1.0,
+ the format is 'g' or 'G', and the alternative format
+ is selected. This means the result must be "1.". */
+ --added_zeros;
hunk. It is IMHO only valid when the the integral part is all nines (or empty),
i.e. when we'll need more int digits, but we don't know this at this point.
For the "%#.0g", 0.956 case which got fixes that is true and similarly for
"%#.4g", 999.98, but not for this "%#.4g", 912.98.

Changed in glibc:
status: Unknown → Confirmed
Revision history for this message
In , Drepper-fsp (drepper-fsp) wrote :

Fixed in cvs.

Changed in glibc:
status: Confirmed → Fix Released
Revision history for this message
Matthias Klose (doko) wrote :

glibc (2.6-5ubuntu1) gutsy; urgency=low

  * Merge with Debian.
    - cvs-printf_fp.c.diff: new patch to print the wrong number of
       digits with %#g. LP: #128355.

 -- Matthias Klose <email address hidden> Sun, 29 Jul 2007 20:39:57 +0200

Changed in glibc:
status: New → Fix Released
Revision history for this message
In , Drepper-fsp (drepper-fsp) wrote :

*** Bug 4869 has been marked as a duplicate of this bug. ***

Revision history for this message
In , Cvs-commit (cvs-commit) wrote :

Subject: Bug 4858

CVSROOT: /cvs/glibc
Module name: libc
Branch: glibc-2_6-branch
Changes by: <email address hidden> 2007-07-31 12:42:39

Modified files:
 . : ChangeLog
 stdio-common : printf_fp.c tfformat.c

Log message:
 2007-07-31 Jakub Jelinek <email address hidden>

 * stdio-common/tfformat.c (sprint_doubles): Add 12 new tests.

 2007-07-28 Ulrich Drepper <email address hidden>

 [BZ #4858]
 * stdio-common/printf_fp.c (___printf_fp): Fix special case of
 #.0g and value rounded to 1.0.
 * stdio-common/tfformat.c (sprint_doubles): Add two new tests.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/ChangeLog.diff?cvsroot=glibc&only_with_tag=glibc-2_6-branch&r1=1.10640.2.1&r2=1.10640.2.2
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/stdio-common/printf_fp.c.diff?cvsroot=glibc&only_with_tag=glibc-2_6-branch&r1=1.63&r2=1.63.2.1
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/stdio-common/tfformat.c.diff?cvsroot=glibc&only_with_tag=glibc-2_6-branch&r1=1.10&r2=1.10.2.1

Revision history for this message
In , Cvs-commit (cvs-commit) wrote :

Subject: Bug 4858

CVSROOT: /cvs/glibc
Module name: libc
Branch: glibc-2_5-branch
Changes by: <email address hidden> 2007-07-31 12:44:28

Modified files:
 . : ChangeLog
 stdio-common : printf_fp.c tfformat.c

Log message:
 2007-07-31 Jakub Jelinek <email address hidden>

 * stdio-common/tfformat.c (sprint_doubles): Add 12 new tests.

 2007-07-28 Ulrich Drepper <email address hidden>

 [BZ #4858]
 * stdio-common/printf_fp.c (___printf_fp): Fix special case of
 #.0g and value rounded to 1.0.
 * stdio-common/tfformat.c (sprint_doubles): Add two new tests.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/ChangeLog.diff?cvsroot=glibc&only_with_tag=glibc-2_5-branch&r1=1.10362.2.96&r2=1.10362.2.97
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/stdio-common/printf_fp.c.diff?cvsroot=glibc&only_with_tag=glibc-2_5-branch&r1=1.58.2.3&r2=1.58.2.4
http://sourceware.org/cgi-bin/cvsweb.cgi/libc/stdio-common/tfformat.c.diff?cvsroot=glibc&only_with_tag=glibc-2_5-branch&r1=1.7.8.2&r2=1.7.8.3

Changed in glibc:
importance: Unknown → Medium
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.