| atof | Function (tigcc.a)

AMS 1.01 or higher | math.h, stdlib.h, timath.h

float atof (const char *s);

Converts a string to a floating point.

atof converts a string pointed to by s to floating point value. It recognizes the character representation of a floating point number, made up of the following:

- an optional string of spaces;
- an optional minus sign;
- a string of digits and an optional decimal point (the digits can be on both sides of the decimal point);
- an optional exponent followed by a (optionally signed) integer.

It is important to say that this implementation of atof requires that an optional minus sign and an optional exponent must be TI Basic characters for minus sign and exponent, (characters with codes 0xAD and 0x95 instead of ordinary '-' and 'e' or 'E' characters). This limitation is caused by using some TIOS calls which needs such number format. Anyway, it is very easy to "preprocess" any string to satisfy this convention before calling to atof by routine like the following (assuming that c is a char variable, and i is an integer variable):

    for (i = 0; (c = s[i]); i++)
     // Yes, the second '=' is really '=', not '=='...
      {
        if (c == '-') s[i] = 0xAD;
        if ((c|32) == 'e') s[i] = 0x95;
      }

atof returns the converted value of the input string. It returns NAN if the input string cannot be converted (i.e. if it is not in a correct format). This is not the same as in ANSI C: atof in ANSI C returns 0 if the conversion was not successful. I decided to return NAN instead, so the user can check whether the conversion was successful (which is not possible with ANSI atof). See is_nan for a good method to check whether the result is NAN.
Note: This function is not part of TIOS, and it is implemented using the TIOS function push_parse_text.

Uses: ER_catch, ER_success, estack_number_to_Float, push_parse_text, top_estack