在+0.0,-0.0什么操作和功能,给出不同的算术结果吗? [英] What operations and functions on +0.0 and -0.0 give different arithmetic results?

查看:141
本文介绍了在+0.0,-0.0什么操作和功能,给出不同的算术结果吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,当±0.0 受支持, -0.0 +0.0 分配到双击通常是没有的算术的区别。虽然他们有不同的位模式,他们算术比较结果为相等。

In C, when ±0.0 is supported, -0.0 or +0.0 assigned to a double typically makes no arithmetic difference. Although they have different bit patterns, they arithmetically compare as equal.

double zp = +0.0;
double zn = -0.0;
printf("0 == memcmp %d\n", 0 == memcmp(&zn, &zp, sizeof zp));// --> 0 == memcmp 0
printf("==          %d\n", zn == zp);                        // --> ==          1

启发由 http://stackoverflow.com/a/25312364/2410359 评论由@Pascal Cuoq,我要寻找一个在标准C多了一些功能,提供了算术不同的结果。

Inspire by a comment in http://stackoverflow.com/a/25312364/2410359 by @Pascal Cuoq, I am looking for a few more functions in standard C that provide arithmetically different results.

请注意:许多功能,如罪(),返回 +0.0 ˚F (+0.0) -0.0 F(-0.0)。但这些不提供不同的运算结果。另外,2结果不应同时 NaN的

Note: Many functions, like sin(), return +0.0 from f(+0.0) and -0.0 from f(-0.0). But these do not provide different arithmetic results. Also the 2 results should not both be NaN.

推荐答案

有一些标准的操作和功能,形成F的数值不同的答案(+0.0) F(-0.0)

There are a few standard operations and functions that form numerically different answers between f(+0.0) and f(-0.0).

不同的舍入的模式或其他浮点运算的实现可能会给出不同的结果。

Different rounding modes or other floating point implementations may give different results.

#include <math.h>

double inverse(double x) { return 1/x; }

double atan2m1(double y) { return atan2(y, -1.0); }

double sprintf_d(double x) {
  char buf[20];
  // sprintf(buf, "%+f", x);   Changed to e
  sprintf(buf, "%+e", x);
  return buf[0];  // returns `+` or `-`
}

double copysign_1(double x) { return copysign(1.0, x); }

double signbit_d(double x) {
  int sign = signbit(x);  // my compile returns 0 or INT_MIN
  return sign;
}

double pow_m1(double x) { return pow(x, -1.0); }

void zero_test(const char *name, double (*f)(double)) {
  double fzp = (f)(+0.0);
  double fzn = (f)(-0.0);
  int differ = fzp != fzn;
  if (fzp != fzp && fzn != fzn) differ = 0;  // if both NAN
  printf("%-15s  f(+0):%-+15e %s  f(-0):%-+15e\n", 
      name, fzp, differ ? "!=" : "==", fzn);
}

void zero_tests(void) {
  zero_test("1/x",             inverse);
  zero_test("atan2(x,-1)",     atan2m1);
  zero_test("printf(\"%+e\")", sprintf_d);
  zero_test("copysign(x,1)",   copysign_1);
  zero_test("signbit()",       signbit_d);
  zero_test("pow(x,-odd)",     pow_m1);;  // @Pascal Cuoq
  zero_test("tgamma(x)",       tgamma);  // @vinc17 @Pascal Cuoq
}


Output:
1/x              f(+0):+inf             !=  f(-0):-inf           
atan2(x,-1)      f(+0):+3.141593e+00    !=  f(-0):-3.141593e+00  
printf("%+e")    f(+0):+4.300000e+01    !=  f(-0):+4.500000e+01   
copysign(x,1)    f(+0):+1.000000e+00    !=  f(-0):-1.000000e+00  
signbit()        f(+0):+0.000000e+00    !=  f(-0):-2.147484e+09 
pow(x,-odd)      f(+0):+inf             !=  f(-0):-inf           
tgamma(x)        f(+0):+inf             !=  f(-0):+inf  


注:结果
tgamma(X)想出了 == 我的gcc 4.8.2的机器上,但是的正确 != 。


Notes:
tgamma(x) came up == on my gcc 4.8.2 machine, but correctly != on others.

rsqrt(),又名 1 /开方()是一个也许未来的C标准的功能。可以/不可以也行。

rsqrt(), AKA 1/sqrt() is a maybe future C standard function. May/may not also work.

双零= +0.0;的memcpy(安培;零&安培; X,sizeof的X)可显示 X 不同的位模式+0.0 ,但 X 可能仍然是一个 +0.0 。我觉得有些FP格式必须是 +0.0 -0.0 许多位模式。 TBD。

double zero = +0.0; memcpy(&zero, &x, sizeof x) can show x is a different bit pattern than +0.0 but x could still be a +0.0. I think some FP formats have many bit patterns that are +0.0 and -0.0. TBD.

这是由 http://stackoverflow.com/help/self-answer 提供了一个自我的答案。

This is a self-answer as provided by http://stackoverflow.com/help/self-answer.

这篇关于在+0.0,-0.0什么操作和功能,给出不同的算术结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆