This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/5044] New: printf doesn't take the rounding mode into account


When printing out the value of a double, e.g. with %f, the rounding mode isn't
taken into account. For instance, the program below:

#include <stdio.h>
#include <fenv.h>

static volatile double x = 1.0 / 3.0;

void out (const char *s, int r)
{
  if (fesetround (r))
    fprintf (stderr, "fesetround error\n");
  else
    printf ("1/3 rounded %s: %1.6f\n", s, x);
}

int main (void)
{
  out ("downward", FE_DOWNWARD);
  out ("  upward", FE_UPWARD);
  return 0;
}

gives:

1/3 rounded downward: 0.333333
1/3 rounded   upward: 0.333333

Since (1.0/3.0) isn't exactly representable in decimal, the rounded decimal
values should be different (more precisely, they should be 0.333333 and 0.333334).

The ISO C standard says in 7.19.6.1#13:

"For e, E, f, F, g, and G conversions, if the number of significant decimal
digits is at most DECIMAL_DIG, then the result should be correctly rounded.243)
If the number of significant decimal digits is more than DECIMAL_DIG but the
source value is exactly representable with DECIMAL_DIG digits, then the result
should be an exact representation with trailing zeros. Otherwise, the source
value is bounded by two adjacent decimal strings L < U, both having DECIMAL_DIG
significant digits; the value of the resultant decimal string D should satisfy L
 <= D <= U, with the extra stipulation that the error should have a correct sign
for the current rounding direction."

-- 
           Summary: printf doesn't take the rounding mode into account
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
        AssignedTo: drepper at redhat dot com
        ReportedBy: vincent+libc at vinc17 dot org
                CC: glibc-bugs at sources dot redhat dot com
  GCC host triplet: i686-pc-linux-gnu, powerpc-unknown-linux-gnu


http://sourceware.org/bugzilla/show_bug.cgi?id=5044

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]