DSP

atof

2019-07-13 14:58发布

atof

double atof(const char *str);
1. Convert string to double
        Parses the C string str, interpreting its content as a floating point number and returns its value as a double.
        The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax resembling that of floating point literals (see below), and interprets them as a numerical value. The rest of the string after the last valid character is ignored and has no effect on the behavior of this function.
1.1 C99/C11 (C++11)
        A valid floating point number for atof using the "C" locale is formed by an optional sign character (+ or -), followed by one of:
- A sequence of digits, optionally containing a decimal-point character (.), optionally followed by an exponent part (an e or E character followed by an optional sign and a sequence of digits).
- A 0x or 0X prefix, then a sequence of hexadecimal digits (as in isxdigit) optionally containing a period which separates the whole and fractional number parts. Optionally followed by a power of 2 exponent (a p or P character followed by an optional sign and a sequence of hexadecimal digits).
- INF or INFINITY (ignoring case).
- NAN or NANsequence (ignoring case), where sequence is a sequence of characters, where each character is either an alphanumeric character (as in isalnum) or the underscore character (_).
        If the first sequence of non-whitespace characters in str does not form a valid floating-point number as just defined, or if no such sequence exists because either str is empty or contains only whitespace characters, no conversion is performed and the function returns 0.0.
1.2 Parameters
str
        C-string beginning with the representation of a floating-point number.
1.3 Return Value
        On success, the function returns the converted floating point number as a double value.
        If no valid conversion could be performed, the function returns zero (0.0).
        If the converted value would be out of the range of representable values by a double, it causes undefined behavior. See strtod for a more robust cross-platform alternative when this is a possibility.
1.4 Data races
        The array pointed by str is accessed.
1.5 Exceptions (C++)
        No-throw guarantee: this function never throws exceptions.
        If str does not point to a valid C-string, or if the converted value would be out of the range of values representable by a double, it causes undefined behavior.
2. Example
2.1 example

/* ============================================================================ Name : atof_example_1.c Author : foreverstrong Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ /* atof example: sine calculator */ #include /* printf, fgets */ #include /* atof */ #include /* sin */ int main() { double n, m; double pi = 3.1415926535; char buffer[256]; printf("Enter degrees: "); fgets(buffer, 256, stdin); n = atof(buffer); m = sin(n * pi / 180); printf("The sine of %f degrees is %f ", n, m); return 0; } Output:
Enter degrees: 0.12 The sine of 0.120000 degrees is 0.002094 Enter degrees: abc0.12 The sine of 0.000000 degrees is 0.000000 Enter degrees: 0.12ab3 The sine of 0.120000 degrees is 0.002094 3. atof
        定义于头文件
double atof(const char *str);
        转译 str 所指向的字节字符串中的浮点值。
        函数会舍弃任何空白符 (由 std::isspace() 确定),直至找到首个非空白符。然后它会连续取用尽可能多的字符,以构成合法的浮点数表示,并将它们转换成浮点值。
        合法的浮点值可以为下列之一:
        十进制浮点数表达式。它由下列部分组成:
(可选) 正或负号
非空的十进制数字序列,可选地包含一个小数点字符 (由当前的 C 本地环境确定) (定义有效数字)
(可选) eE,并跟随可选的正或负号,以及非空十进制数字序列 (定义指数)
        二进制浮点数表达式。它由下列部分组成:
(可选) 正或负号
0x0X
非空的十六进制数字序列,可选地包含一个小数点字符 (由当前的 C 本地环境确定) (定义有效数字)
(可选) p P,并跟随可选的正或负号,以及非空十进制数字序列 (定义指数)
        无穷大表达式。它由下列部分组成:
(可选) 正或负号
INFINFINITY,忽略大小写
        非数 (NaN) 表达式。它由下列部分组成:
(可选) 正或负号
NANNAN (char_sequence),忽略 NAN 部分的大小写。char_sequence 只能由数字、拉丁字母和下划线构成。结果是一个安静的 NaN 浮点值。
任何其他可由当前 C 本地环境接受的表达式
3.1 Parameters
        str - 指向待转译的空终止字符串的指针
3.2 Return Value
        成功时返回代表 str 内容的 double 值。若转换的值在返回值范围外,则返回值未定义。若无可进行的转换,则返回 0.0。
4. Example
4.1 example

/* ============================================================================ Name : atof_example_2.c Author : foreverstrong Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ /* atof example: sine calculator */ #include /* printf, fgets */ #include /* atof */ int main(void) { printf("%g ", atof(" -0.0000000123junk")); printf("%g ", atof("0.012")); printf("%g ", atof("15e16")); printf("%g ", atof("-0x1afp-2")); printf("%g ", atof("inF")); printf("%g ", atof("Nan")); printf("%g ", atof("1.0e+309")); printf("%g ", atof("0.0")); printf("%g ", atof("junk")); printf("%g ", atof("0.12j3unk")); return 0; } Output:-1.23e-08 0.012 1.5e+17 -107.75 inf nan inf 0 0 0.12
References
http://www.cplusplus.com/reference/cstdlib/atof/
http://zh.cppreference.com/w/c/string/byte/atofhttp://en.cppreference.com/w/c/string/byte/atof


热门文章