- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下代码:
#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_DIG
或 log10(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/
这个问题已经有答案了: Is floating point math broken? (33 个回答) 已关闭 7 年前。 我的 strtod() 有问题似乎添加了一些数字。我正在阅读 2\t5241
这个问题已经有答案了: Is floating point math broken? (33 个回答) 已关闭 7 年前。 我有一个有趣的程序,我想给出一个参数,以便它将触发最后一个 else 条件来
我正在学习 Bjarne Stroustrup 的《编程 - 原则与实践》,一个相当简单的练习让我感到难过(这只是第 11 部分中的第 2 部分)。 #include "../../std_lib_f
我正在学习 C++,并且正在完成一些示例练习。特别是这个接受命令行参数,然后对它们进行数学运算,这样如果我输入 ./ex1 sum 1.0 2.0 3.0 它应该对数字求和。我的代码是这样的: int
我正在使用 strtod() 将字符串转换为十进制。由于我需要为不正确的输入/无效字符抛出错误,所以我别无选择。 然而,问题是 strtod() 受语言环境影响。所以一个'.'当程序在不同的语言环境中
我有一个缓冲区,其中有“随机”数字,用逗号分隔。例如: “1589.3,12478.359,485.39971”等 我需要检查小数部分和整数部分的长度。 我做了一些研究,然后决定在我的缓冲区上使用 s
我不想不必要地重新发明轮子,但我一直在寻找 strtod 的功能,但有一个基本参数 (2,8,10,16)。 (我知道 strtoul 允许一个基本参数,但我正在寻找返回类型 double)。任何正确
在 C++ (Visual C++ 2010) 中使用 strtod 函数将字符串转换为 double 时,我想检测下溢。尽管我根据 strtod 的文档做了以下代码,但它并没有像我预期的那样工作:
效果很好。 #include #include int main(void){ char number[]= "a123.45", *strtod_eptr; double num;
简单的问题:数字 1.15507e-173 的 double 正确位表示是什么?完整问题:如何确定这个数字的正确解析? 背景:我的问题来自 this answer它显示了来自三个不同解析器的两个不同的
如果我想将 char 数组中的前 3 个字符解析为 double,忽略后面的字符,我真的需要这样做吗? int main() { const char a[] = "1.23"; char
我有以下代码: int check_for_non_number(char *input, int lineNum) { errno = 0; char *endptr; pr
我目前正在为我大学的实验室练习开发一个银行终端程序。 让我大吃一惊的是一个函数,它应该接受用户输入的转账金额,检查它是否符合所有要求,如果符合,则返回提供给程序的值。 我们的导师对所有确保输入安全的方
我正在使用 GNU GCC 编译器在代码块编辑器中编码。我尝试使用以下原型(prototype)的函数 strtod: double strtod(const char *a, char **b);
我的 C 项目遇到了一些麻烦。我使用 fgets(line, 1024, stdin) 读取了一行。在该行中,应该有 4 个用空格分隔的参数,如果没有,程序应该写一个警告。像这样的事情: “1f 2
我一直致力于处理如下所示的输入文件的项目: A 0.0345 B 0.3945 ... Z 0.2055 基本上,我正在阅读每一行,在阅读完该行之后,我想仅从字符串中拉出 double 值。我正在尝试
所以我有这个 Java 程序,我用它来处理数 TB 的数据。性能是一个问题。 我分析了该应用程序,所有内存分配的很大一部分以及 CPU 时间的很大一部分来自执行一个简单的操作: 我有一个 ASCII
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 #include ... cin >> str; errno = 0 ; double d = strtod(str
我正在将部分 C++ 程序转换为 Python,但我在替换 C 函数时遇到了一些问题 strtod .我正在处理的字符串由简单的数学方程式组成,例如“KM/1000.0”。问题是常量和数字混合在一起,
我有一个库需要解析总是使用点“.”的双数。作为小数分隔符。不幸的是,对于这种情况,strtod() 尊重可能使用不同分隔符的语言环境,因此解析可能会失败。我不能 setlocale() - 它不是线程
我是一名优秀的程序员,十分优秀!