gpt4 book ai didi

c - 如何使用DBL_MANT_DIG检查strtod

转载 作者:行者123 更新时间:2023-11-30 15:49:59 24 4
gpt4 key购买 nike

考虑以下代码:

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

int main (void) {
double val;
/* base b = 2; 2^DBL_MANT_DIG */
/* decimal digits log10(2^DBL_MANT_DIG) */
/*const char *str = "9007199254740992";*/
const char *str = "9007199254740993";

errno = 0;
val = strtod(str, NULL);

printf("%d\n", DBL_MANT_DIG );

if (errno == ERANGE) {
printf("error\n");
} else {
printf("%f\n", val);
}

return 0;
}

这将返回:

53
9007199254740992.000000

由于 str 的字符串数字的有效位数超出了我的机器可以处理的位数,因此如何使用 DBL_MANT_DIGlog10(2^DBL_MANT_DIG) 版本它来检查 val 的结果是否正确?

最佳答案

您不使用它们来检查转换是否准确。

Here's one way of how to do it .

另一种方法是find out how many decimal digits after the decimal point are there in the resultant double ,使用它作为精度执行 sprintf() 并将其输出与原始字符串进行比较。

关于c - 如何使用DBL_MANT_DIG检查strtod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15998705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com