This is the mail archive of the
newlib@sources.redhat.com
mailing list for the newlib project.
printf bug: printf("%.f",0.1) == 2 !?
- From: Honda Hiroki <hhonda at ipflex dot com>
- To: newlib at sources dot redhat dot com
- Date: Tue, 29 Jul 2003 19:28:44 +0900 (JST)
- Subject: printf bug: printf("%.f",0.1) == 2 !?
Hi,
I ported newlib-1.11.0 to an inhouse embedded system (with an original
CPU) and noticed that
#include <stdio.h>
char buf[16];
int main(void) {
int n = sprintf(buf, "%.f", 0.1);
printf("[%s] %d\n", buf, n); /* should print "[0] 1" */
return 0;
}
prints "[0] 2".
The attached one-line patch fixed the problem,
but I am not sure whether or not the patch has evil side effects.
Is there any moderator who will investigate this problem?
Regards,
====
HONDA Hiroki
R&D Center, IP Flex Inc.
http://www.ipflex.com/english/index.html
================================================================
--- newlib-1.11.0/newlib/libc/stdio/vfprintf.c.orig 2 Jul 2003 02:27:30 -0000
+++ newlib-1.11.0/newlib/libc/stdio/vfprintf.c 29 Jul 2003 10:04:04 -0000
@@ -830,7 +830,7 @@
if (prec || flags & ALT)
size += prec + 1;
} else /* "0.X" */
- size = prec + 2;
+ size = (prec || flags & ALT) ? 2 + prec : 1;
} else if (expt >= ndig) { /* fixed g fmt */
size = expt;
if (flags & ALT)