的考慮:輸出FMOD功能C++
#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;
}
鑑於int(x/h) == 10
,我本來期望的fmod()
結果接近0,但我得到的是0.0999999999。這是一個重大的差異。餘數()的結果仍然可以接受。代碼可以嘗試在http://ideone.com/9wBlva
爲什麼fmod()結果有顯着差異?
C++和C Arent使用相同的語言 – amanuel2
如果這樣不能自己回答您的問題,請閱讀文檔。 'fmod'和'remaining'不會做同樣的事情,所以會有不同的結果。 –
@ amanuel2你得到了類似的結果http://ideone.com/rLyS2t – umbersar