problem with atof / sscanf / strtof and float's range

Manuel Novoa III mjn3 at codepoet.org
Sat Dec 2 22:11:58 UTC 2006


On Thu, Nov 30, 2006 at 08:42:59AM +0100, Mathias.Krause at de.telex.com wrote:
> > > Hi Developpers,
> > > 
> > > I have a problem using the functions sscanf, atof and strtof of uClibc 
> to
> > > convert a string to a float value.
> > > 
> > > [...]
> > > 
> > > so my question is: is it possible for me to detect the range overflow 
> of
> > > my float? With standard libc sscanf would return 0 to indicate the 
> error.
> > 
> > As far as I know this is simply a bug in uClibc. It puts arbitrary
> > limits on the size of the float strings it will read IIRC.

There is a limit on the number of _significant_ digits _considered_ in the
conversion, since you can only represent so many in a float.  But all digits
are read.

> Sorry, but I have to ask again:
> Can you tell me what is the right thing to do now?
> 
> Is there a newer version than the one included with my ucLinux-dist 
> (uClibc 0.9.27) where this bug is solved?
> I think there is 0.9.28! How can i find out if the bug is fixed? 
> Or is there a workaround for the problem?
> 
> Thanks for your help!

You're test works find for me.  scanf, atof, and strtof all convert to
inf for me on mipsel, since your value is larger than the max
representable by a float.  But while the standard guarantees that strtof
will set errno to ERANGE in this case (which I see in my tests), it does
not make such guarantees with either scanf or atof.

That leaves the question... why are you seeing -0.000000 from printf.  What
arch are you running on?

Manuel



More information about the uClibc mailing list