问题
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