Needed help for strtof return value

M

Marcus Jacobs

Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands
of times. Because of this, it is not feasible to check all of the
messages to see if the conversion failed. Does anyone have any ideas
as to how I can test for a successful strtof conversion for my
situation.


Regards

Marcus D. Jacobs
 
R

Ronald Landheer-Cieslak

Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands
of times. Because of this, it is not feasible to check all of the
messages to see if the conversion failed. Does anyone have any ideas
as to how I can test for a successful strtof conversion for my
situation.
Sure: compare the string you tried to convert with 0.000000. If you can have
any number of zeroes before and after the dot, you can do something like this:

int is_zero(const char * str)
{
int i;
int found_dot = 0;
for (i = 0; str; i++)
{
if (str == '.')
{
if (found_dot)
return -1; /* error */
found_dot++;
continue;
}
if (str != '0')
return(0);
}

return(1);
}

HTH

rlc

--
Jail: Just Another Interpreted Language
Just: Jail Uses Silly Terms

Join the discussion on the definition of this language at
(e-mail address removed) http://jail-ust.sourceforge.net
(send mail to (e-mail address removed))
 
A

A. Sinan Unur

(e-mail address removed) (Marcus Jacobs) wrote in
Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message.

Returning zero is not the only thing strtof does upon failing. Read the
docs about the second parameter. Then, do a proper error check in your code
rather than one that only goes halfway.

Sinan.
 
I

Irrwahn Grausewitz

Dear Group

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands
of times. Because of this, it is not feasible to check all of the
messages to see if the conversion failed. Does anyone have any ideas
as to how I can test for a successful strtof conversion for my
situation.

The following may help you to solve this problem:

<quote>

From ISO/IEC 9899:1999

7.20.1.3 The strtod, strtof, and strtold functions
[...]
float strtof(const char * restrict nptr, char ** restrict endptr);
[...]
#7
If the subject sequence is empty or does not have the expected form, no
conversion is performed; the value of nptr is stored in the object
pointed to by endptr, provided that endptr is not a null pointer.
[...]
#10
The functions return the converted value, if any. If no conversion could
be performed, zero is returned. If the correct value is outside the
range of representable values, plus or minus HUGE_VAL, HUGE_VALF, or
HUGE_VALL is returned (according to the return type and sign of the
value), and the value of the macro ERANGE is stored in errno. If the
result underflows (7.12.1), the functions return a value whose magnitude
is no greater than the smallest normalized positive number in the return
type; whether errno acquires the value ERANGE is implementation-defined.

</quote>

Thus, you can check errno to see if an error occurred, and you can
examine endptr to check how much of your string was processed.

Regards

Irrwahn
 
R

Robert B. Clark

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error

Perhaps you meant strtod?
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands

strtod is prototyped as

double strtod(const char *s, char **endptr)

On error, *endptr (if not NULL) points to the character that failed the
conversion. So, instead of relying on the return value from strtod, use
the endptr argument:

#include <stdio.h>
#include <stdlib.h>

int cvtfstr(const char *s, float *num, char **errptr)
{
char *endptr;

*errptr = NULL;
*num = (float) strtod(s, &endptr);

/* On error (endptr not at end of string), return zero */
/* Also set errptr to point to errant character */

if (*endptr != '\0')
{
*errptr = endptr;
return 0;
}

return 1;
}


#define NUM_NUMSTR 10

int main(void)
{
const char *numstr[NUM_NUMSTR] =
{
"22.32873", "0.00", "23-skiddoo",
"1954", ".732", " 13 ",
" 13", "-1234.56e-2", "+ 951",
"+159"
};

size_t i;
float num;
char *errptr;

for (i = 0; i < NUM_NUMSTR; i++)
{
printf("s=\"%s\"\t\tnum=", numstr);

if (cvtfstr(numstr, &num, &errptr))
printf("%f\n", num);
else
printf("ERROR AT OFFSET %p (%s)\n",
errptr - numstr, errptr);
}

return 0;
}
 
L

lawrence.jones

Marcus Jacobs said:
The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message.

As you've discovered, checking the return value is not a good way to
determine whether the conversion was successful or not. That's what the
second argument is for:

char *endptr;

f = strtof(buf, &endptr);
if (endptr == buf) // an error occurred

-Larry Jones

Well of course the zipper's going to get stuck if everyone
stands around WATCHING me! -- Calvin
 
K

Keith Thompson

I have written a file conversion program that uses strtof to convert
text strings to floats. It works as I intended except for my error
messages. It is my understanding that strtof returns the converted
value if the conversion is successful a "0" upon failure. When this
conversion fails, my program sends an error message to my error.log
file. The problem with this is there are times when strtof converts a
text string such as "0.000000" to a float. The conversion is
successful but when the program goes to test whether or not the
conversion is successful, since the returned value is equal to "0" it
send out an error message. This can happen hundreds if not thousands
of times. Because of this, it is not feasible to check all of the
messages to see if the conversion failed. Does anyone have any ideas
as to how I can test for a successful strtof conversion for my
situation.

The strtof() function takes two arguments, a char* pointing to the
string to be converted, and a char** pointing to a pointer which is
set to point past the converted string.

For example:

char *s = "123 456";
char *endptr;
float f = strtof(s, &endptr);

This will set f to 123.0, and set endptr to point to the blank in the
string s (i.e., just past the portion of the input string that it was
able to convert).

If strtof() wasn't able to make a conversion (i.e., the argument
string doesn't start with zero or more whitespace characters followed
by a valid floating-point constant), strtof() will return 0.0 and
endptr will be set to point to the beginning of the argument string.

After the call, if endptr == s, the call failed; otherwise, it
succeeded.

You can also check errno to determine why the call failed (assuming
you've set errno to 0 before the call), but remember that errno is not
set if the call fails because the string didn't contain a
floating-point constant.
 
K

Keith Thompson

Robert B. Clark said:
Perhaps you meant strtod?

The C90 standard defined the strtod function (returning a double).
The C99 standard adds strtof (returning a float) and strtold
(returning a long double) functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top