我想通过C++找出float和double类型的机器epsilon,但我对于变量x
每种数据类型都一次又一次地得到相同的答案。使用,这是长双和O(1e-20)的顺序。我在使用Codeblocks的Windows 10机器上运行它。无法在代码块中找到C++中float float的机器epsilon
我试过在Ubuntu中使用相同的代码,也在Windows本身的DevC++中,我得到了正确的答案。什么是我在代码块中做错了。有没有默认设置?
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
int main()
{
//double x = 5;
//double one = 1;
//double fac = 0.5;
float x=1;
float one = 1.0;
float fac = 0.5;
// cout <<"What is the input of number you are giving"<< endl;
// cin >> x;
cout <<"The no. you have given is: "<< x << endl;
int iter = 1;
while(one+x != one)
{
x = x * fac;
iter = iter + 1;
}
cout<<"The value of machine epsilon for the given data type is "<<x<<endl;
cout<<"The no.of iterations taken place are: "<<iter<<endl;
}
'机器epsilon浮法和双类型',它不是它的工作方式。在2附近浮动或双倍之间,epsilon不是一个常数。 float和double不是以整数形式存储的。 –
您正在使用的epsilon的确切定义是什么,以及您的算法与此有关?这就是说,你的问题是无关紧要的,请参阅发布指南。 –
你能给我们提供codeblocks使用的完整编译选项吗? (如快速数学,O3等)。 – fjardon