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

Mathias Krause mathias.krause at de.telex.com
Wed Nov 29 10:31:39 UTC 2006


Hi Developpers,

I have a problem using the functions sscanf, atof and strtof of uClibc to
convert a string to a float value.

For example, I do the following to convert my huge string:
--- code ---
  float myFloat = 0.0;
  char myString[] = {"9999999999999999999999999999999999999999999999999.9"};
  int ret = 0;

  ret = sscanf(myString, "%f", &myFloat);
  // myFloat = atof(myString);
  // myFloat = strtof(myString, NULL);

  printf("Return: %i\r\n", ret);
  printf("Float: %f\r\n", myFloat);
--- end code ---

If the Number in the String is not as big as in my example, everything
works fine. But for such a big number, the application outputs
--- output ---
Return: 1
Float: -0.000000
--- output end ---

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.

I also tried with atof, but with the same problem
--- code ---
  float myFloat = 0.0;
  char myString[] = {"9999999999999999999999999999999999999999999999999.9"};

  myFloat = atof(myString);

  printf("Float: %f\r\n", myFloat);
--- end code ---

--- output ---
Float: -0.000000
--- end output ---

And with strtof the same:
--- code ---
  float myFloat = 0.0;
  char myString[] = {"9999999999999999999999999999999999999999999999999.9"};

  myFloat = strtof(myString, NULL);

  printf("Float: %f\r\n", myFloat);
--- end code ---
--- output ---
Float: -0.000000
--- end output ---

So can anybody tell me, how i can recognize that an error has occured? Or
is the value of myFloat a special value like "INV" or "HUGE_VAL" or
something like this?

By the way I am using uCLinux (dist 20060803) with kernel 2.4 and uClibc
for a Coldfire processor.

Thanks for your help
Mathias Krause



More information about the uClibc mailing list