dsaff82024 2017-07-26 14:06
浏览 25
已采纳

为什么2.4 mod 0.8返回0.8 [重复]

This question already has an answer here:

Why modulus calculation of 2.4 by 0.8 returns 0.8? Shouldn't it return 0?

It happens both in PHP, via fmod, and JavaScript, via %.

Even a google research with this operation fails: http://www.google.com/search?q=2.4+mod+0.8

</div>
  • 写回答

2条回答 默认 最新

  • douhang5460 2017-07-26 21:50
    关注

    If you look closer at what happens in this concrete example, the influence of the binary format becomes more visible.

    The input is stored as a binary floating point approximation. This then can be printed as decimal number uniquely identifying the binary,

    In [1]: print "%.25f"%2.4
    2.3999999999999999111821580
    
    In [2]: print "%.25f"%0.8
    0.8000000000000000444089210
    

    The first step is the computation of the quotient and of its integer part. Obviously, as the stored numbers are smaller 2.4 and larger 0.8, the quotient is smaller than 3,

    In [3]: print "%.25f"%(2.4/0.8)
    2.9999999999999995559107901
    

    The modulo result, the remainder, is then finally the difference after removing the integer multiple,

    In [4]: print "%.25f"%(2.4-2*0.8)
    0.7999999999999998223643161
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计