相关疑难解决方法(0)

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;
}
Run Code Online (Sandbox Code Playgroud)

鉴于int(x/h) == 10,我本来希望fmod()结果接近 0,但我得到的是 .0999999999。这是一个显着的差异。余数()的结果似乎仍然可以接受。可以在http://ideone.com/9wBlva尝试代码

为什么 fmod() 结果存在显着差异?

c++ floating-point fmod

5
推荐指数
1
解决办法
2097
查看次数

什么代表C++中的Math.IEEERemainder(x,y)?

什么代表C++中的Math.IEEERemainder(x,y)

c++

4
推荐指数
2
解决办法
956
查看次数

标签 统计

c++ ×2

floating-point ×1

fmod ×1