- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
Machine-Epsilon 似乎有两个定义:
首先,我看不出这两者之间有何关联。第二个 DBL_EPSILON 在我的理解中不符合定义 2:
以下程序打印:
DBL_EPSILON: 2.220446049250313080847e-16
DBL_EPSILON / 2: 1.110223024625156540424e-16
1.0 + DBL_EPSILON: 1.000000000000000222045e+00
1.0 + DBL_EPSILON / 2: 1.000000000000000000000e+00
m_eps 2.220446049250313080847e-16
m_eps -1u 2.220446049250312834328e-16
1.0 + m_eps -1u 1.000000000000000222045e+00
(m_eps -1u < DBL_EPSILON): True
(m_eps -1u == DBL_EPSILON/2): False
m_eps -1u
应该是一个更小但非常接近 DBL_EPSILON
的数字。和定义 2) 1.0 + m_eps -1u
不应计算为 1.0
?为什么有必要为此将 DBL_EPSILON
设为 2?
#include <stdout.h>
#include <stdint.h>
#inlcude <float.h>
union Double_t {
double f;
int64_t i;
};
int main(int argc, char *argv[])
{
union Double_t m_eps;
printf("DBL_EPSILON: \t\t%.*e\n", DECIMAL_DIG, DBL_EPSILON);
printf("DBL_EPSILON / 2: \t%.*e\n", DECIMAL_DIG, DBL_EPSILON / 2);
printf("1.0 + DBL_EPSILON: \t%.*e\n", DECIMAL_DIG, 1.0 + DBL_EPSILON);
printf("1.0 + DBL_EPSILON / 2: \t%.*e\n", DECIMAL_DIG, 1.0 + DBL_EPSILON / 2);
m_eps.f = DBL_EPSILON;
printf("\nm_eps \t\t\t%.*e\n", DECIMAL_DIG, m_eps.f);
m_eps.i -= 1;
printf("m_eps -1u\t\t%.*e\n", DECIMAL_DIG, m_eps.f);
printf("\n1.0 + (m_eps -1u)\t\t%.*e\n", DECIMAL_DIG, 1.0 + m_eps.f);
printf("\n(m_eps -1u < DBL_EPSILON): %s\n",
(m_eps.f < DBL_EPSILON) ? "True": "False"
);
printf("(m_eps -1u == DBL_EPSILON/2): %s\n",
(DBL_EPSILON/2 == m_eps.f) ? "True": "False"
);
return 0;
}
最佳答案
DBL_EPSILON
的错误定义,您引用为“满足 1.0 + machine_eps != 1 的最小正数”的定义正在四处流传。您甚至可以在标准库和 StackOverflow 上的其他很好的答案中找到它。当在标准库中找到时,它在一个明显不对应于注释但对应于正确定义的值附近的注释中:
DBL_EPSILON: This is the difference between 1 and the smallest floating point number of type
double
that is greater than 1. (correct definition taken from the GNU C library)
C99 标准短语是这样的:
the difference between 1 and the least value greater than 1 that is representable in the given floating point type, b^(1−p)
这可能是您困惑的原因。忘记错误的定义。我写了一篇关于这个的咆哮here (这很像你的问题)。
您问题中的另一个定义“将实数舍入为下一个 float 时的最大相对误差”在舍入结果为正常 float 时是正确的。将实数四舍五入为有限 float 会产生与实数相差 1/2 ULP 以内的 float 。对于一个普通的 float ,这个 1/2 ULP 绝对误差转化为一个相对误差,它可以在 DBL_EPSILON/2 和 DBL_EPSILON/4 之间,具体取决于 float 在其 binade 中的位置。 .
关于关于 C 语言中 DBL_EPSILON 和 Machine-Epsilon 的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24584140/
DBL_EPSILON/std::numeric_limits::epsilon 会给我最小的值,当加一时会有所不同。 我无法理解如何将这些知识应用到有用的东西中。 epsilon 比计算机可以处理的
我正在使用这个- #define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( do
我想编写以下程序:我想扫描三角形三边的长度(以浮点形式表示)。然后我想再次扫描第二个三角形的三个边。接下来的事情是找出这两个三角形是否相似。这意味着如果第一个三角形中的一条边与第二个三角形中的相同边的
我在 python 中使用 OpenCV (4.0.0) 的 solvePnPRansac 函数。有时(但并非总是)当我将点传递给函数时,我会收到错误消息: (-215:Assertion faile
Machine-Epsilon 似乎有两个定义: 将实数四舍五入到下一个 float 时的最大相对误差。 满足 1.0 + machine_eps != 1.0 的最小正数 首先,我看不出这两者之间有
我在 Linux (AS 3) 上使用 GCC 3.4 并试图找出获得 DBL_EPSILON,或者至少是一个不错的近似值。如何以编程方式获取它? 最佳答案 在 C++ 中是 std::numeric
我是一名优秀的程序员,十分优秀!