小编Jor*_*son的帖子

将C字符串转换为/从双精度转换时的奇怪行为

我无法理解C的规则,即在打印双精度或将字符串转换为双精度时要考虑的精度.以下程序应说明我的观点:

#include <errno.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv) {
    double x, y;
    const char *s = "1e-310";

    /* Should print zero */
    x = DBL_MIN/100.;
    printf("DBL_MIN = %e, x = %e\n", DBL_MIN, x);

    /* Trying to read in floating point number smaller than DBL_MIN gives an error */
    y = strtod(s, NULL);
    if(errno != 0)
        printf("  Error converting '%s': %s\n", s, strerror(errno));
    printf("y = %e\n", y);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我编译并运行这个程序时得到的输出(在带有gcc 4.5.2的Core 2 …

c printf ieee-754 strtod

7
推荐指数
2
解决办法
384
查看次数

标签 统计

c ×1

ieee-754 ×1

printf ×1

strtod ×1