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/4943] New: Inconsistent rounding behaviour for sprintf and IEEE doubles


sample source code and output demonstrating the problem:

#include <stdio.h>
#include <math.h>

inline double dp(double d, int p)
{
  double f = pow(10, p);
  return round(d * f) / f;
}
  
int main(int a_argc, char *a_argv[])
{
  char l_buf[256];
  double d = 2597.525;
  sprintf(l_buf, "%.15f", d);  printf("a) %.3f=%s\n", d, l_buf);  
  sprintf(l_buf, "%.2f", d);   printf("b) %.3f=%s\n", d, l_buf);
  sprintf(l_buf, "%.2f", dp(d, 2));  printf("c) %.3f=%s\n", d, l_buf);
  d = 2597.625;
  sprintf(l_buf, "%.15f", d);  printf("d) %.3f=%s\n", d, l_buf);  
  sprintf(l_buf, "%.2f", d);   printf("e) %.3f=%s\n", d, l_buf);
  sprintf(l_buf, "%.2f", dp(d, 2));  printf("f) %.3f=%s\n", d, l_buf);
  return 0;
}

Output:
a) 2597.525=2597.525000000000091
b) 2597.525=2597.53
c) 2597.525=2597.53
d) 2597.625=2597.625000000000000
e) 2597.625=2597.62
f) 2597.625=2597.63

Note that where the value 2597.625 is stored precisely, the output for sprintf
is inconsistent with that expected for the default selected IEEE rounding mode.

-- 
           Summary: Inconsistent rounding behaviour for sprintf and IEEE
                    doubles
           Product: glibc
           Version: 2.3.4
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
        AssignedTo: drepper at redhat dot com
        ReportedBy: paul at inet dot co dot za
                CC: glibc-bugs at sources dot redhat dot com
 GCC build triplet: N/A
  GCC host triplet: Linux
GCC target triplet: i386


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

------- 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]