question on wrong result for small numbers from strtod(), strtof()

Howland Craig D (Craig)
Mon Oct 25 16:57:00 GMT 2010

     I have taken a look at it, and although not familiar with the
routine, I think that I found the problem.  However, I don't have
a platform to test on with 32-bit double.  Please try the attached
addition and let me know how it works.
     (This fix is really a bit of a kludge, cleaning up a problem
after it was made rather than fixing the root cause, but this is
much easier, as stopping it from happening is much more work.)
     Given the nature of the mistake in strtod, it would not
be entirely surprising if other cases also caused problems.  I
don't know if you have them already or not, but some additional
test cases for things like overflows, underflows, and subnormals
seem like a good idea (e.g. -1.2345678e39, 8.7654321e-45, and

-----Original Message-----
From: []
On Behalf Of Daniel Calcoen
Sent: Friday, October 22, 2010 10:37 AM
Subject: question on wrong result for small numbers from strtod(),

I'm compiling for Renesas RX with gcc 4.5 (Newlib 1.18.0)
As RX floating point unit is 32bits the compiler defines double=float

( the following is exactly the same for strtod() )
When I do a strof() for the string "1.17549434E-37"
I end with the float 1.175494e-035, where the mantissa is ok but the
exponent is wrong.

following the code of strtod.c at line 442

	k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;

k is 7 (nd=9 and DBL_DIG=6)
then goes to line 523

	e1 += nd - k;

where e1 (-45) is adjusted to -43 
(the mantissa was converted correctly, rv.d=1.1754943e+8)

then the approximation will end with 1.175494e-035 (instead of

I don't understand all the details of the precision check and the
approximation that follows so I looking for help to solve my problem.

When I compile for double=64 bits and float=32bits so DBL_DIG=15 the
are correct.

Daniel Calcoen
-------------- next part --------------
A non-text attachment was scrubbed...
Name: strtod.patch
Type: application/octet-stream
Size: 663 bytes
Desc: strtod.patch
URL: <>

More information about the Newlib mailing list