您的位置:首页 > 其它

一个数值计算中通常很有用的数值的定义和计算

2015-06-11 14:38 281 查看
matlab中直接用eps,默认是针对double双精度类型的.

微软的msdn中是这么提的:

numeric_limits::epsilon


The function returns the difference between 1 and the smallest value greater than 1 that is representable for the data type.

The difference between 1 and the smallest value greater than 1 that is representable for the data type.

代码例子:

// numeric_limits_epsilon.cpp
// compile with: /EHsc
#include <iostream>
#include <limits>

using namespace std;

int main( )
{
   cout << "The difference between 1 and the smallest "
        << "value greater than 1\n for float objects is: " 
        << numeric_limits<float>::epsilon( ) 
        << endl;
   cout << "The difference between 1 and the smallest "
        << "value greater than 1\n for double objects is: " 
        << numeric_limits<double>::epsilon( ) 
        << endl;
   cout << "The difference between 1 and the smallest "
        << "value greater than 1\n for long double objects is: " 
        << numeric_limits<long double>::epsilon( ) 
        << endl;
}


输出结果是:

The difference between 1 and the smallest value greater than 1

for float objects is: 1.19209e-007

The difference between 1 and the smallest value greater than 1

for double objects is: 2.22045e-016

The difference between 1 and the smallest value greater than 1

for long double objects is: 2.22045e-016

维基中有这样一个表格:

Values for standard hardware floating point arithmetics

http://eigen.tuxfamily.org/index.php?title=Main_Page

The following values of machine epsilon apply to standard floating point formats:

IEEE 754 - 2008Common nameC++ data typeBase

Precision

Machine epsilon[a]

Machine epsilon

binary16half precisionshort211 (one bit is implicit)2−11 = 4.88e-042−10 = 9.77e-04
binary32single precisionfloat224 (one bit is implicit)2−24 = 5.96e-082−23 = 1.19e-07
binary64double precisiondouble253 (one bit is implicit)2−53 = 1.11e-162−52 = 2.22e-16
binary80extended precision_float80[1]2642−64 = 5.42e-202−63 = 1.08e-19
binary128quad(ruple) precision_float128[1]2113 (one bit is implicit)2−113 = 9.63e-352−112 = 1.93e-34
decimal32single precision decimal_Decimal32[2]1075 × 10−710−6
decimal64double precision decimal_Decimal64[2]10165 × 10−1610−15
decimal128quad(ruple) precision decimal_Decimal128[2]10345 × 10−3410−33
[b]a according to Prof. Demmel,LAPACK,Scilabb
according to Prof. Higham; ISO C standard;C,

C++ and
Python language constants;
Mathematica,
MATLAB and
Octave; various textbooks - see below for the latter definition

如果使用了GMP/MPIR/MPFR之类的扩展软件工具来提高实际参与计算的精度, 用Pavel的C++ wrapper mpfrC++(很奇怪他的个人主页只涉及技术相关信息也被屏蔽了), 结合Eigen
C++ template library是我的最爱.
https://code.google.com/p/gmpy/downloads/detail?name=full-src-mpir-mpfr-mpc-gmpy2-2.0.2.zip&can=2&q=
在Linux, MacOS上这类库的源代码直接编译通常没有问题, 在windows下稍微有些麻烦. 这里有一个修改后的,可以在visual studio下面直接编译成动态和静态链接库的项目文件的包,mpir,mpfr,mpc,gmpy都包括了. 我把下载所需资源分数设置为10,实际上,正常评论之后根据规则这个资源分都会自动返还而且会额外加分的.
所以下载的时候别有心理压力. 实在接受不了,可以到code.google.com的上面的链接中去下载. 没有任何问题.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: