不搞数值计算的人可能不会注意这个问题。
在大部分情况下很多人不会用到小数点后16位,但是程序中要求中间变量保持16位以上的精度,很多情况下是必须的。我发现VC6.0以及VS2003,双精度数据的最高精度是16位,某些情况下(比如自适应算法)这会降低程序的运行速度,比如用积分法计算伽玛函数,计算gamma(26.9)
mathematica4.2:
2.9065239044018627e+26
Dev-C++ 4.9.8.10:
2.906523904401857e+026
BCBX1.0:
2.906523904401856818e+26
VC6.0:
2.9065239043927034e+026而是用一下代码计算结果也有区别(算法本身提供的精度不高)long double GammaLn( long double x )
{
if ( x < 0 ) exit( 0 );
if ( x == 0 ) return 0;
long double xbuf, y, tmp, ser;
static long double cof[6] =
{
76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.001208650973866179, -0.000005395239384953
};
int i;
y = xbuf = x;
tmp = xbuf + 5.5;
tmp -= ( xbuf + 0.5 ) * log( tmp );
for ( ser = 1.000000000190015, i = 0; i <= 5; i++ )
{
y++;
ser += cof[i] / y;
}
return ( -tmp + logl( 2.5066282746310005 * ser / xbuf ) );}//---------------------------------------------------------------------------
long double Gamma( long double x )
{
long double temp1, temp2;
if ( x < 0 )
{
if ( int( x ) == x ) return 1.0e300;
temp1 = fabs( x );
temp2 = expl( GammaLn( temp1 ) );
return -PI / ( x * temp2 * sinl( PI * x ) );
}
else
return ( expl( GammaLn( x ) ) );
}
结果是:
Dev-C++
2.906523904463532e+026
BCBX1.0
2.906523904463532204e+26
VC6.0
2.9065239044635298e+026VC在浮点数上需要进行某种设置吗?请朋友告知,谢谢!
在大部分情况下很多人不会用到小数点后16位,但是程序中要求中间变量保持16位以上的精度,很多情况下是必须的。我发现VC6.0以及VS2003,双精度数据的最高精度是16位,某些情况下(比如自适应算法)这会降低程序的运行速度,比如用积分法计算伽玛函数,计算gamma(26.9)
mathematica4.2:
2.9065239044018627e+26
Dev-C++ 4.9.8.10:
2.906523904401857e+026
BCBX1.0:
2.906523904401856818e+26
VC6.0:
2.9065239043927034e+026而是用一下代码计算结果也有区别(算法本身提供的精度不高)long double GammaLn( long double x )
{
if ( x < 0 ) exit( 0 );
if ( x == 0 ) return 0;
long double xbuf, y, tmp, ser;
static long double cof[6] =
{
76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.001208650973866179, -0.000005395239384953
};
int i;
y = xbuf = x;
tmp = xbuf + 5.5;
tmp -= ( xbuf + 0.5 ) * log( tmp );
for ( ser = 1.000000000190015, i = 0; i <= 5; i++ )
{
y++;
ser += cof[i] / y;
}
return ( -tmp + logl( 2.5066282746310005 * ser / xbuf ) );}//---------------------------------------------------------------------------
long double Gamma( long double x )
{
long double temp1, temp2;
if ( x < 0 )
{
if ( int( x ) == x ) return 1.0e300;
temp1 = fabs( x );
temp2 = expl( GammaLn( temp1 ) );
return -PI / ( x * temp2 * sinl( PI * x ) );
}
else
return ( expl( GammaLn( x ) ) );
}
结果是:
Dev-C++
2.906523904463532e+026
BCBX1.0
2.906523904463532204e+26
VC6.0
2.9065239044635298e+026VC在浮点数上需要进行某种设置吗?请朋友告知,谢谢!
在大部分情况下很多人不会用到小数点后16位,但是程序中要求中间变量保持16位以上的精度,很多情况下是必须的。
我发现VC6.0以及VS2003,双精度数据的最高精度是16位,某些情况下(比如自适应算法)这会降低程序的运行速度,比如用积分法计算伽玛函数,计算gamma(26.9)
mathematica4.2:
2.9065239044018627e+26
Dev-C++ 4.9.8.10:
2.906523904401857e+026
BCBX1.0:
2.906523904401856818e+26
VC6.0:
2.9065239043927034e+026
而用一下代码计算结果也有区别(算法本身提供的精度不高)
long double GammaLn( long double x )
{
if ( x < 0 ) exit( 0 );
if ( x == 0 ) return 0;
long double xbuf, y, tmp, ser;
static long double cof[6] =
{
76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.001208650973866179, -0.000005395239384953
};
int i;
y = xbuf = x;
tmp = xbuf + 5.5;
tmp -= ( xbuf + 0.5 ) * log( tmp );
for ( ser = 1.000000000190015, i = 0; i <= 5; i++ )
{
y++;
ser += cof[i] / y;
}
return ( -tmp + logl( 2.5066282746310005 * ser / xbuf ) );
}
//---------------------------------------------------------------------------
long double Gamma( long double x )
{
long double temp1, temp2;
if ( x < 0 )
{
if ( int( x ) == x ) return 1.0e300;
temp1 = fabs( x );
temp2 = expl( GammaLn( temp1 ) );
return -PI / ( x * temp2 * sinl( PI * x ) );
}
else
return ( expl( GammaLn( x ) ) );
}
结果是:
Dev-C++
2.906523904463532e+026
BCBX1.0
2.906523904463532204e+26
VC6.0
2.9065239044635298e+026
VC在浮点数上需要进行某种设置吗?请朋友告知,谢谢!
您应该使用更高精度的浮点数,
如果系统本身没有提供更高精度的浮点数,
您可能需要自己实现。好象有一种 long double