Validating a string which represents a double ???????



  • : : :
    : : : What sscanf() does after it's done parsing is to call the strtof(), strtol() etc functions. The same is done from the atof/atoi functions, with base 10 as default.
    : : :
    : :
    : : sscanf() approach is a much neater and shorter solution to verify if
    : : a given string is double or not compared to the one that converts the
    : : string to float, prints it into a string, and then compares the two
    : : strings.
    : :
    : That's not what I'm arguing about. What I'm saying is that you use sscanf() where atof would have given exactly the same result, with less overhead.
    : And as I said, that code with sprintf() has more functionality than the one with sscanf(). It will detect if there is any junk letters at the end, which the sscanf() code will not.
    : Summary:
    : To parse a double from a string with no care taken of junk letters, use atof() or strtof().
    : If you want to detect junk letters, you might use the sprintf() version I wrote, sence it will do a check if the string is identical after the conversion.
    : Now if you really want high performance you would write a custom function that checks for digits and other acceptable float characters in the string. All printf/scanf functions are incredible slow.
    : But if you want high performance you should avoid float numbers in the first place, and instead try to find a solution with plain integers, which is often possible.

    Ok, got your point. Thanks for the explanation.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!