Floating points wrapped in Sum and Product even if it breaks associativity?

旧城冷巷雨未停 提交于 2019-12-11 08:50:09

问题


Trying to learn Haskell and stumbled upon this:

Prelude> import Data.Semigroup
Prelude Data.Semigroup> let x = Sum 0.1 <> Sum 0.2 <> Sum 0.3
Prelude Data.Semigroup> let y = (Sum 0.1 <> Sum 0.2) <> Sum 0.3
Prelude Data.Semigroup> x == y
False

Obviously that's the normal inaccuracy with floating point arithmetic, but why are floating point values instances of Num or perhaps why is there an instance instance Num a => Semigroup (Sum a) if associativity doesn't hold? Are there any other areas where the guarantees of the type system aren't guarantees that one should be aware of? Besides fixed-width numerical values?

来源:https://stackoverflow.com/questions/45805440/floating-points-wrapped-in-sum-and-product-even-if-it-breaks-associativity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!