鉴于:
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;
int main() {
// your code goes here
double h = .1;
double x = 1;
int nSteps = abs(x / h);
double rem = fmod(x, h);
cout<<"fmod output is "<<rem<<endl;
if(abs(rem)<std::numeric_limits<double>::epsilon())
cout<<"fmod output is almost near 0"<<endl;
rem = remainder(x,h);
cout<<"remainder output is "<<rem<<endl;
if(abs(rem)<std::numeric_limits<double>::epsilon())
cout<<"remainder output is almost near 0"<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
鉴于int(x/h) == 10,我本来希望fmod()结果接近 0,但我得到的是 .0999999999。这是一个显着的差异。余数()的结果似乎仍然可以接受。可以在http://ideone.com/9wBlva尝试代码
为什么 fmod() 结果存在显着差异?