代码之家  ›  专栏  ›  技术社区  ›  ludo

Haskell挂在数字转换上

  •  1
  • ludo  · 技术社区  · 7 年前

    我有以下代码,在使用GHC编译后,在使用运行时似乎始终挂起(尽管使用 -Werror

    import Data.Aeson
    import Data.Scientific
    import qualified Data.HashMap.Strict as S
    
    myObj = Object $
      S.fromList [("bla", Number $ pc * 100.0)]
      where pc = 10 / 9   
    

    在尝试访问时 myObj 9 高于a 10

    1 回复  |  直到 7 年前
        1
  •  5
  •   erisco    7 年前

    转换 10 % 9 (理性的)科学是不会终止的。

    10 / 9 :: Scientific
    

    From the documentation of Data.Scientific

    仅部分定义!具体来说,recip和/或will Difference 十进制扩展。当输入有理时,fromRational将发散 具有无限十进制展开。考虑使用fromRationalRepetend 从哪里开始。

    因此,请尝试以下方法:

    let Right (x, _) = fromRationalRepetend Nothing (10 / 9) in x
    

    你必须决定哪些措施是合适的。我决定在这里忽略 Left .