在使用中,double fma(double x, double y, double z);我希望d下面标有的输出行中有一个非零值'?'.它似乎在内部只使用long double精度,而不是无限精度的规定.
的
fma函数计算(x×y)+z,四舍五入一个三元操作:它们计算的值(仿佛)到无限精度和圆一次的结果的格式,根据当前的舍入模式.§7.12.13.12(我的重点)
那我的fma()坏了,或者我在代码或编译选项中如何错误地使用它?
#include <float.h>
#include <math.h>
#include <stdio.h>
int main(void) {
// Invoking: Cygwin C Compiler
// gcc -std=c11 -O0 -g3 -pedantic -Wall -Wextra -Wconversion -c -fmessage-length=0
// -v -MMD -MP -MF"x.d" -MT"x.o" -o "x.o" "../x.c"
printf("FLT_EVAL_METHOD %d\n", FLT_EVAL_METHOD);
for (unsigned i = 20; i < 55; i++) …Run Code Online (Sandbox Code Playgroud) 我正在执行这个问题的代码:为什么以下代码的输出不为零?
#include <stdio.h>
int main (void)
{
double A = 373737.0;
double B;
B = A*A*A + 0.37/A - A*A*A - 0.37/A;
printf("The value of B is %f.\n", B);
}
Run Code Online (Sandbox Code Playgroud)
每个主流 x86 编译器的每个优化设置都会给出输出-0.000001。当我使用当前的 clang 15.0.0 时,我-O0也得到了这一点。
但是,使用 14.0.0 版本以上的 clang 和-O1to进行编译-O3会给出输出-1.000001。为什么会发生这种情况?这是一个已知的错误?
Godbolt 为了您的方便:https ://godbolt.org/z/M5j3fGhWf
两个乘积之差和两个乘积之和是在各种常见计算中发现的两个原语。diff_of_products (a,b,c,d) := ab - cd 和 sum_of_products(a,b,c,d) := ab + cd 是密切相关的伴随函数,仅部分操作数的符号不同。使用这些原语的示例是:
\n计算 x = (a + i b) 和 y = (c + i d)的复数乘法:
\nx*y = diff_of_products (a, c, b, d) + i sum_of_products (a, d, b, c)
\n计算 2x2 矩阵的行列式:diff_of_products (a, d, b, c):
\n| a b |\n| c d |\nRun Code Online (Sandbox Code Playgroud)\n在直角三角形中,计算斜边和相邻内切线的相对内切线的长度: diff_of_products (h, h, a, a)ha
计算具有正判别式的二次方程的两个实数解:
\nq = -(b + copysign …