4

sqrt()の長いdoubleバージョンの精度に問題があることに気づきました。次のコードは、問題を示しています。

#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()の通常のdoubleバージョンは、実際の値に最も近いdoubleを提供します。

私が使用している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) 

これは既知のバグですか?これをどこかに報告する必要がありますか?

4

2 に答える 2

9

ここには2つの問題があります。1つは、戻り値をに割り当てても、低精度バージョンのを使用している場合でも、3L暗黙的にそうしdoubleないようにプロモートすることです。引数として3をする必要があります。次に、Cでは関数のオーバーロードがサポートされていないため、のバージョンのみがグローバル名前空間にインポートされます。代わりにを使用する必要があります。long doublelong doublesqrtstatic_castlong doubledoublesqrtstd::sqrt

したがって:

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

于 2011-01-27T16:13:40.193 に答える
0

'regular'バージョンのdoublesqrt()の値は、long doubleよりも大きな丸めの粒度を経験していますか?これは私たちが期待することです。この「きめ細かい」丸めがたまたま正しい値に近づいている可能性があります-長い二重平方根よりも近いです。

これを確認する方法は、複数の値を試して比較することです。

于 2011-01-27T15:59:20.427 に答える