Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 长双sqrt()的精度_C++_Double_Long Integer_Sqrt - Fatal编程技术网

C++ 长双sqrt()的精度

C++ 长双sqrt()的精度,c++,double,long-integer,sqrt,C++,Double,Long Integer,Sqrt,我注意到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

我注意到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;
}
给予

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) 

这是已知的bug吗?我应该在某个地方报告吗?

double sqrt()的“常规”版本的值是否比长双精度的值具有更大的舍入粒度?这是我们所期望的。这可能是因为这种“颗粒”舍入恰好接近正确的值——比长双sqrt更接近


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

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

因此:


long double s=std::sqrt(静态_cast(3))

对代码稍作修改,将s的值调整为1+LDBL_ε或1-LDBL_ε的系数,我用2、3、5和11测试了sqrt()的准确性。在每种情况下,double sqrt()都给出了正确的结果,而长的double sqrt()被大量关闭——在sqrt(2L)的情况下,需要892次乘以1-LDBL_EPSILON才能得到最准确的值!啊,是的,这就是问题所在。谢谢。:-)
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.
>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)