为什么我从std :: fmod和std :: remainder获得不同的结果 [英] Why am I getting a different result from std::fmod and std::remainder

查看:925
本文介绍了为什么我从std :: fmod和std :: remainder获得不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例应用中,我用 953 0.1 计算浮点余数$ c> std :: fmod



我所期望的是,因为 953.0 / 0.1 == 9530 std :: fmod(953,0.1)== 0



0.1 - 为什么会这样?



请注意, $ c> std :: remainder 我得到正确的结果。



这是:

  std :: fmod(953,0.1)== 0.1 // unexpected 
std :: remainder(953,0.1)== 0 //预期



两个函数之间的区别:



根据 cppreference.com




  • std :: fmod 计算以下内容:



  • 正确的值 x - n * y ,其中 n x / y / p>


    • std :: remainder 计算如下:



    正确的值 x - n * y ,其中 n 是最接近准确值的整数值 x / y



    两个函数具有相同的输出。为什么不是这样?



    范例应用程式:



      #include< iostream> 
    #include< cmath>

    bool is_zero(double in)
    {
    return std :: fabs(in)< 0.0000001;
    }

    int main()
    {
    double numerator = 953;
    double denominator = 0.1;

    double quotient = numerator / denominator;
    double fmod = std :: fmod(molecular,denominator);
    double rem = std :: remainder(numerator,denominator);

    if(is_zero(fmod))
    fmod = 0;
    if(is_zero(rem))
    rem = 0;

    std :: cout<< quotient:<商<< ,fmod:< fmod<< ,rem:< rem< std :: endl;
    return 0;
    }



    输出:



     商:9530,fmod:0.1,rem:0 


    解决方案

    欢迎来到浮点数学。这里是发生了什么:十分之一不能精确地表示在二进制,正如三分之一不能精确地表示十进制。结果,除法产生的结果略低于9530.底操作产生整数9529而不是9530.然后剩下0.1剩余。


    In the below example app I calculate the floating point remainder from dividing 953 by 0.1, using std::fmod

    What I was expecting is that since 953.0 / 0.1 == 9530, that std::fmod(953, 0.1) == 0

    I'm getting 0.1 - why is this the case?

    Note that with std::remainder I get the correct result.

    That is:

    std::fmod     (953, 0.1) == 0.1 // unexpected
    std::remainder(953, 0.1) == 0   // expected
    

    Difference between the two functions:

    According to cppreference.com

    • std::fmod calculates the following:

    exactly the value x - n*y, where n is x/y with its fractional part truncated

    • std::remainder calculates the following:

    exactly the value x - n*y, where n is the integral value nearest the exact value x/y

    Given my inputs I would expect both functions to have the same output. Why is this not the case?

    Exemplar app:

    #include <iostream>
    #include <cmath>
    
    bool is_zero(double in)
    {
        return std::fabs(in) < 0.0000001;
    }
    
    int main()
    {
        double numerator   = 953;
        double denominator = 0.1;
    
        double quotient = numerator / denominator;
        double fmod     = std::fmod     (numerator, denominator);
        double rem      = std::remainder(numerator, denominator);
    
        if (is_zero(fmod))
            fmod = 0;
        if (is_zero(rem))
            rem = 0;
    
        std::cout << "quotient: " << quotient << ", fmod: " << fmod << ", rem: " << rem << std::endl;
        return 0;
    }
    

    Output:

    quotient: 9530, fmod: 0.1, rem: 0
    

    解决方案

    Welcome to floating point math. Here's what happens: One tenth cannot be represented exactly in binary, just as one third cannot be represented exactly in decimal. As a result, the division produces a result slightly below 9530. The floor operation produces the integer 9529 instead of 9530. And then this leaves 0.1 left over.

    这篇关于为什么我从std :: fmod和std :: remainder获得不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆