非整数模/余数函数 [英] Modulus/Remainder function for non-integers

查看:180
本文介绍了非整数模/余数函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rem给出了这个:

  Prelude> rem 9 8 
1

我想要这样的东西:

  Prelude> nonIntRem 9.1 8 
1.0999999999999996

我是这样实现的:

  nonIntRem xy = x  - (y *(fromIntegral $ truncate(x / y)))

我的问题是:


  1. 像这样的东西已经存在于一个标准哈斯克尔图书馆?我宁愿使用标准功能,我可能错过了它。

  2. 如果没有,其他语言的这个函数是否有更多的标准名称?也许fmod,但在这种情况下负面行为不是像mod,而是像rem。如果没有标准名称,你可以想一个更好的名字为这个函数?
  3. 它似乎工作正常,但如果你注意到这个函数的问题,我想
  4. ://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Fixed.html#v:mod-39-rel =noreferrer> mod' 来自 Data.Fixed


    rem gives this:

    Prelude> rem 9 8
    1
    

    I wanted something like this:

    Prelude> nonIntRem 9.1 8
    1.0999999999999996
    

    I implemented it like this:

    nonIntRem x y = x - (y * (fromIntegral $ truncate (x/y)))
    

    My questions are:

    1. Does something like this already exist in a standard Haskell library? I'd prefer to use a standard function, and I may have missed it.
    2. If not, is there a more standard name for this function in other languages? Maybe fmod, but the behavior for negatives in this case is not like mod, but like rem. If there is no standard name, can you think of a better name for this function?
    3. It seems to work properly, but if you notice a problem with this function, I'd like to know about it.

    解决方案

    The function you're after is mod' from Data.Fixed.

    这篇关于非整数模/余数函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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