long double sqrt() 的准确性

发布于 2024-10-14 14:17:17 字数 1989 浏览 7 评论 0原文

我注意到 sqrt() 的长双精度版本的准确性存在问题。下面的代码演示了这个问题。

#include <iostream>
#include <cmath>
#include <cfloat>

int main(int argc, char ** argv)
{
  int count=0;
  long double s=sqrt(3L);
  std::cout.precision(21);
  std::cout << "s=" << s << ", s^2=" << s*s << std::endl;
  while( s*s<3L+LDBL_EPSILON ) {
    s+=LDBL_EPSILON;
    std::cout << s << ' ' << s*s << std::endl;
    ++count;
  }
  std::cout << "sqrt(3L) is approximately " << count << " multiples of LDBL_EPSILON away from the correct value." << std::endl;
  return 0;
}

编译并运行它

>g++ -o sqrt sqrt.cpp && ./sqrt

给出

s=1.73205080756887719318, s^2=2.9999999999999996524
1.73205080756887719329 2.99999999999999965284
1.73205080756887719339 2.99999999999999965306
... (922 lines omitted)
1.73205080756887729347 2.99999999999999999978
1.73205080756887729357 3.00000000000000000022
sqrt(3L) is approximately 926 multiples of LDBL_EPSILON away from the correct value.

sqrt() 的常规双精度版本给出最接近实际值的双精度值。

我正在使用的 g++ 版本是

>g++ -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8) 

这是一个已知的错误吗?我应该在某处报告此事吗?

I have noticed a problem with the accuracy of the long double version of sqrt(). The following code demonstrates the problem.

#include <iostream>
#include <cmath>
#include <cfloat>

int main(int argc, char ** argv)
{
  int count=0;
  long double s=sqrt(3L);
  std::cout.precision(21);
  std::cout << "s=" << s << ", s^2=" << s*s << std::endl;
  while( s*s<3L+LDBL_EPSILON ) {
    s+=LDBL_EPSILON;
    std::cout << s << ' ' << s*s << std::endl;
    ++count;
  }
  std::cout << "sqrt(3L) is approximately " << count << " multiples of LDBL_EPSILON away from the correct value." << std::endl;
  return 0;
}

Compiling and running this with

>g++ -o sqrt sqrt.cpp && ./sqrt

gives

s=1.73205080756887719318, s^2=2.9999999999999996524
1.73205080756887719329 2.99999999999999965284
1.73205080756887719339 2.99999999999999965306
... (922 lines omitted)
1.73205080756887729347 2.99999999999999999978
1.73205080756887729357 3.00000000000000000022
sqrt(3L) is approximately 926 multiples of LDBL_EPSILON away from the correct value.

The regular double version of sqrt() gives the double closest to the real value.

The version of g++ I'm using is

>g++ -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8) 

Is this a known bug? Should I report this somewhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤蝉 2024-10-21 14:17:17

这里有两个问题:首先,3L隐式提升为double而不是long double,所以即使您将返回值分配给long double 它仍然使用 sqrt 的低精度版本。您需要将 static_cast 3 转换为 long double 作为参数。其次,只有 sqrtdouble 版本被导入到全局命名空间中,因为 C 中不支持函数重载,您必须使用 std::sqrt< /代码> 相反。

因此:

long double s=std::sqrt(static_cast(3));

You have two issues here: First, 3L implicitly promotes to double not long double so even though you assign the return value to a long double it's still using the low precision version of sqrt. You'll need to static_cast 3 to long double as the argument. Secondly, only the double version of sqrt is imported into the global namespace because function overloading isn't supported in C, you have to use std::sqrt instead.

Thus:

long double s=std::sqrt(static_cast<long double>(3));

゛时过境迁 2024-10-21 14:17:17

double sqrt() 的“常规”版本中的值是否比 long double 经历了更大的舍入粒度?这是我们所期望的。这种“粒度”舍入可能恰好接近正确值 - 比长双开方更接近。

检查这一点的方法是尝试多个值并进行比较。

Is the value from the 'regular' version of double sqrt() experiencing a greater granularity of rounding than the long double? This is what we would expect. It could be that this 'granular' rounding happens to be hitting close to the correct value - closer than the long double sqrt.

The way to check this would be to try multiple values and compare.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文