public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libc/16151] New: strtod() incorrectly rounds 2^-1075 to 0x0.0000000000001p-1022
@ 2013-11-11  4:06 exploringbinary at gmail dot com
  2013-11-11 18:29 ` [Bug libc/16151] " jsm28 at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: exploringbinary at gmail dot com @ 2013-11-11  4:06 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=16151

            Bug ID: 16151
           Summary: strtod() incorrectly rounds 2^-1075 to
                    0x0.0000000000001p-1022
           Product: glibc
           Version: 2.18
            Status: NEW
          Severity: minor
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: exploringbinary at gmail dot com
                CC: drepper.fsp at gmail dot com

Given the decimal string that represents 2^-1075, strtod() returns an
incorrectly rounded result: it returns 0x0.0000000000001p-1022 instead of the
correct answer, 0x0p+0 (zero). 2^-1075 is half the smallest double precision
value, 2^-1074.

I did a little tracing to help isolate the bug. It appears that the division is
correct and that this is just a rounding problem. In /stdlib/strtod_l.c,
round_and_return() calls round_away() with half_bit == true, last_digit_odd ==
false, and more_bits == true. This causes it to round up (case FE_TONEAREST).
more_bits should be false, which would make it round correctly to even. 

This program demonstrates the problem:

#include <stdio.h>
#include <stdlib.h>
int main (void)
{ 
 double d =
strtod("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024703282292062327208828439643411068618252990130716238221279284125033775363510437593264991818081799618989828234772285886546332835517796989819938739800539093906315035659515570226392290858392449105184435931802849936536152500319370457678249219365623669863658480757001585769269903706311928279558551332927834338409351978015531246597263579574622766465272827220056374006485499977096599470454020828166226237857393450736339007967761930577506740176324673600968951340535537458516661134223766678604162159680461914467291840300530057530849048765391711386591646239524912623653881879636239373280423891018672348497668235089863388587925628302755995657524455507255189313690836254779186948667994968324049705821028513185451396213837722826145437693412532098591327667236328125",NULL);
 printf("%a\n",d);
}

(I used PARI/GP to generate that string: 2.^-1075)


(I am submitting this bug report on behalf of Water Qian, who discovered this
bug and reported it on my blog:
http://www.exploringbinary.com/how-glibc-strtod-works/#comment-6892 .)

-- 
You are receiving this mail because:
You are on the CC list for the bug.


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-06-13  9:33 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-11  4:06 [Bug libc/16151] New: strtod() incorrectly rounds 2^-1075 to 0x0.0000000000001p-1022 exploringbinary at gmail dot com
2013-11-11 18:29 ` [Bug libc/16151] " jsm28 at gcc dot gnu.org
2013-11-11 18:41 ` jsm28 at gcc dot gnu.org
2013-11-12 16:48 ` exploringbinary at gmail dot com
2013-11-13 13:01 ` cvs-commit at gcc dot gnu.org
2013-11-13 13:02 ` jsm28 at gcc dot gnu.org
2013-11-13 14:15 ` exploringbinary at gmail dot com
2014-06-13  9:33 ` fweimer at redhat dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).