Re: Floating point exception in strtod()
- Date: Mon, 9 Apr 2018 08:09:20 -0400
- From: Ken Brown <kbrown@xxxxxxxxxxx>
- Subject: Re: Floating point exception in strtod()
On 4/9/2018 5:47 AM, Corinna Vinschen wrote:
On Apr 7 13:40, Ken Brown wrote:
$ cat strtod_test.c
/* The following number comes from /usr/share/asymptote/ode.asy. */
const char *str = "121645100408832000.0";
strtod (str, &ptr);
/* If there was an exception, the following will not get executed. */
printf ("No exception.\n");
$ gcc strtod_test.c
Floating point exception (core dumped)
[The above was on x86. On x86_64 there's simply no output.]
I have no idea what's special about the number 121645100408832000.0, but the
problem goes away if, for example, I replace the leading 1 by 2.
GDB shows that the exception occurs in newlib/libc/stdlib/strtod.c
line 1189, in this statment, which looks rather inconspicious at
L = (Long)aadj;
L is of type Long == int32_t, aadj is of type double. The
value of aadj at this time is 2529648000.0 == 0x96c75d80 which
appears to be perfectly valid for a 32 bit int.
However, on 64 bit for example the assembler statement generating the FP
It is documented that this statemnt may raise FE_INVALID or FE_INEXACT
exceptions. The problem is that the generated 32 bit value is a
negative signed int value, while the source operand is positive. So the
conversion is, in fact, invalid.
I applied a fix to newlib's strtod, to always use 64 bit ints in this
place. This fixes the problem and no exception is raised.
Thanks for the explanation and the quick fix. I'm sorry for causing
confusion by also sending this to the newlib list.
Problem reports: http://cygwin.com/problems.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple