This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug libc/4943] New: Inconsistent rounding behaviour for sprintf and IEEE doubles
- From: "paul at inet dot co dot za" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sources dot redhat dot com
- Date: 20 Aug 2007 14:22:06 -0000
- Subject: [Bug libc/4943] New: Inconsistent rounding behaviour for sprintf and IEEE doubles
- Reply-to: sourceware-bugzilla at sourceware dot org
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.