- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 Debian 系统上 C 数学库的 GCC 实现显然具有符合 (IEEE 754-2008) 的函数实现exp
,暗示四舍五入应始终正确:
(from Wikipedia) The IEEE floating point standard guarantees that add, subtract, multiply, divide, fused multiply–add, square root, and floating point remainder will give the correctly rounded result of the infinite precision operation. No such guarantee was given in the 1985 standard for more complex functions and they are typically only accurate to within the last bit at best. However, the 2008 standard guarantees that conforming implementations will give correctly rounded results which respect the active rounding mode; implementation of the functions, however, is optional.
事实证明,我遇到了这个功能实际上有阻碍的情况,因为 exp
的确切结果函数通常几乎恰好位于两个连续 double
之间的中间值 (1),然后程序进行大量的进一步计算,速度损失高达 400 (!) 倍:这实际上是对我的解释(病态的:-S)Question #43530011 .
(1) 更准确地说,这发生在 exp
的参数时结果是 (2 k + 1) × 2-53 的形式,其中 k 是一个相当小的整数(例如 242) .特别是,pow (1. + x, 0.5)
涉及的计算倾向于打电话 exp
当 x
时有这样的论点数量级为2-44。
由于在某些情况下正确舍入的实现可能非常耗时,我猜开发人员也会设计出一种方法来获得稍微不太精确的结果(比如,最多只能达到 0.6 ULP 或类似的东西)在给定范围内参数的每个值(大致)有界的时间内......(2)
……但是怎么做呢??
(2) 我的意思是我只是不希望参数的某些异常值像 (2 k + 1) × 2-53 会是比大多数相同数量级的值更耗时;但我当然不介意参数的某些特殊值是否变得更快,或者大参数(绝对值)是否需要更长的计算时间。
这是一个显示该现象的最小程序:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
int main (void)
{
int i;
double a, c;
c = 0;
clock_t start = clock ();
for (i = 0; i < 1e6; ++i) // Doing a large number of times the same type of computation with different values, to smoothen random fluctuations.
{
a = (double) (1 + 2 * (rand () % 0x400)) / 0x20000000000000; // "a" has only a few significant digits, and its last non-zero digit is at (fixed-point) position 53.
c += exp (a); // Just to be sure that the compiler will actually perform the computation of exp (a).
}
clock_t stop = clock ();
printf ("%e\n", c); // Just to be sure that the compiler will actually perform the computation.
printf ("Clock time spent: %d\n", stop - start);
return 0;
}
现在在 gcc -std=c99 program53.c -lm -o program53
之后:
$ ./program53
1.000000e+06
Clock time spent: 13470008
$ ./program53
1.000000e+06
Clock time spent: 13292721
$ ./program53
1.000000e+06
Clock time spent: 13201616
另一方面,program52
和 program54
(通过将 0x20000000000000
分别替换为 0x10000000000000
和 0x40000000000000
得到):
$ ./program52
1.000000e+06
Clock time spent: 83594
$ ./program52
1.000000e+06
Clock time spent: 69095
$ ./program52
1.000000e+06
Clock time spent: 54694
$ ./program54
1.000000e+06
Clock time spent: 86151
$ ./program54
1.000000e+06
Clock time spent: 74209
$ ./program54
1.000000e+06
Clock time spent: 78612
注意,这种现象是依赖于实现的!显然,在常见的实现中,只有 Debian 系统(包括 Ubuntu)显示这种现象。
P.-S.:我希望我的问题不是重复的:我彻底搜索了一个类似的问题但没有成功,但也许我确实注意到使用了相关的关键字...... :-/
最佳答案
float 很难,而且常常违反直觉。不是每个程序员都读过 what they should have .当库过去允许一些稍微不准确的舍入时,人们提示库函数的精度,因为他们不准确的计算不可避免地出错并产生废话。作为回应,图书馆的作者们把他们的图书馆做得恰到好处,所以现在人们不能把责任推给他们。
以exp()
非常接近 0
的数字在 float 中是有问题的,因为结果是接近 1
的数字虽然所有精度都在与一的差中,因此大多数有效数字都丢失了。计算 exp(x) - 1
更精确(并且在此测试用例中明显更快)通过 C 数学库函数 expm1(x)
.如果exp()
本身是真的需要的,它仍然要快得多 expm1(x) + 1
.
计算 log(1 + x)
也存在类似的问题, 其中有函数 log1p(x)
.
加速提供的测试用例的快速修复:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
int main (void)
{
int i;
double a, c;
c = 0;
clock_t start = clock ();
for (i = 0; i < 1e6; ++i) // Doing a large number of times the same type of computation with different values, to smoothen random fluctuations.
{
a = (double) (1 + 2 * (rand () % 0x400)) / 0x20000000000000; // "a" has only a few significant digits, and its last non-zero digit is at (fixed-point) position 53.
c += expm1 (a) + 1; // replace exp() with expm1() + 1
}
clock_t stop = clock ();
printf ("%e\n", c); // Just to be sure that the compiler will actually perform the computation.
printf ("Clock time spent: %d\n", stop - start);
return 0;
}
对于这个案例,我机器上的时间是这样的:
原始代码
1.000000e+06
Clock time spent: 21543338
修改后的代码
1.000000e+06
Clock time spent: 55076
对于有经验的程序员,可以使用 Newton-Raphson、Taylor 或 Maclaurin 多项式等方法编写慢函数的近似实现,特别是来自 Intel 的 MKL、AMD 的 AMCL 等库的不精确舍入的特殊函数,放宽 float -点编译器的标准合规性,将精度降低到 ieee754 binary32 ( float
),或这些的组合。
请注意,更好地描述问题会带来更好的答案。
关于c - 我*不*想要函数 exp 的正确舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44346371/
这个问题已经有答案了: How to do case insensitive string comparison? (23 个回答) 已关闭 3 年前。 用户在我的输入栏中写入“足球”,然后执行第 6
啊,不习惯 javascript 中的字符串。 character_id= + id + correct= + correctOrIncorrect 这就是我需要制作成字符串的内容。如果您无法猜测字符
$(function() { var base_price = 0; CalculatePrice(); $(".math1").on('change', function(e) { Calc
我找不到任何文章回答问题:将Spinnaker部署到Spinnaker将管理的同一Kubernetes集群是否安全/正确?我主要是指生产,HA部署。 最佳答案 我认为Spinnaker和Kuberne
我正在使用MSVC在Windows上从源代码(官方源代码发布,而不是从仓库中)构建Qt5(Qt 5.15.0)。 我正在设置环境。变量,依赖项等,然后运行具有1600万个选项的configure,最后
我需要打印一个包含重复单词的数组。我的数组已经可以工作,但我不知道如何正确计算单词数。我已经知道,当我的索引计数器 (i) 为 49 时,并且当 (i) 想要计数到 50 时,我会收到错误,但我不知道
我正在遵循一个指南,该指南允许 Google map 屏幕根据屏幕尺寸禁用滚动。我唯一挣扎的部分是编写一个代码,当我手动调整屏幕大小时动态更改 True/False 值。 这是我按照说明操作的网站,但
我有一个类“FileButton”。它的目的是将文件链接到 JButton,FileButton 继承自 JButton。子类继承自此以使用链接到按钮的文件做有用的事情。 JingleCardButt
我的 friend 数组只返回一个数字而不是所有数字。 ($myfriends = 3) 应该是…… ($myfriends = 3 5 7 8 9 12). 如果我让它进入 while 循环……整个
这个问题在这里已经有了答案: Is there a workaround to make CSS classes with names that start with numbers valid?
我正在制作一个 JavaScript 函数,当调整窗口大小时,它会自动将 div 的大小调整为与窗口相同的宽度/高度。 该功能非常基本,但我注意到在调整窗口大小时出现明显的“绘制”滞后。在 JS fi
此问题的基本视觉效果可在 http://sevenx.de/demo/bootstrap-carousel/inc.carousel/tabbed-slider.html 获得。 - 如果你想看一看。
我明白,如果我想从函数返回一个字符串文字或一个数组,我应该将其声明为静态的,这样当被调用的函数被返回时,内容就不会“消亡”。 但我的问题是,当我在函数内部使用 malloc 分配内存时会怎样? 在下面
在 mySQL 数据库中存储 true/false/1/0 值最合适(读取数据消耗最少)的数据字段是什么? 我以前使用过一个字符长的 tinyint,但我不确定它是否是最佳解决方案? 谢谢! 最佳答案
我想一次读取并处理CSV文件第一行中的条目(例如打印)。我假设使用Unix风格的\n换行符,没有条目长度超过255个字符,并且(现在)在EOF之前有一个换行符。这意味着它是fgets()后跟strto
所以,我们都知道 -1 > 2u == true 的 C/C++ 有符号/无符号比较规则,并且我有一种情况,我想有效地实现“正确”比较。 我的问题是,考虑到人们熟悉的尽可能多的架构,哪种方法更有效。显
**摘要:**文章的标题看似自相矛盾。 本文分享自华为云社区《Java异常处理:如何写出“正确”但被编译器认为有语法错误的程序》,作者: Jerry Wang 。 文章的标题看似自相矛盾,然而我在“正
我有一个数据框,看起来像: dataDemo % mutate_each(funs(ifelse(. == '.', REF, as.character(.))), -POS) # POS REF
有人可以帮助我使用 VBScript 重新格式化/正确格式化带分隔符的文本文件吗? 我有一个文本文件 ^分界如下: AGREE^NAME^ADD1^ADD2^ADD3^ADD4^PCODE^BAL^A
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我是一名优秀的程序员,十分优秀!