Shouldn't these instances be constrained to Integral types?
Jason
No. It is associative, just not in the way you expect. 😄
> On 25 September 2014 18:54, Carter Schonwald <carter.s...@gmail.com> wrote:
> No. It is associative, just not in the way you expect. 😄
>
> From What Every Computer Scientist Should Know About Floating-Point Arithmetic by David Goldberg: "Due to roundoff errors, the associative laws of algebra do not necessarily hold for floating-point numbers. For example, the expression (x+y)+z has a totally different answer than x+(y+z) when x = 10^30, y = -10^30 and z = 1 (it is 1 in the former case, 0 in the latter)."
Knuth Volume 2, second edition:
(u [*] v) [*] w = uvw(1+d1)(1+d2)
u [*] (v [*] w) = uvw(1+d3)(1+d4)
where each |di| < 1/2 b**(1-p), so
(u [*] v) [*] w
--------------- = 1 + d
u [*] (v [*] w)
where |d| < 2 b**(1-p)/(1 - 1/2 b**(1-p))**2
= 2 ulp/(1 - 1/2 ulp)**2
subject to certain caveats. It's not unfair to say that
floating point multiplication is (nearly) associative
"within a few ulp".
Goldberg's example involves addition, not multiplication.
(Checking addition of numbers *with the same sign* is left
as an exercise for the reader.)
subject to certain caveats. It's not unfair to say that
floating point multiplication is (nearly) associative
"within a few ulp".
_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
For anything to be a Monoid (or any type class with laws), you implicitly have a definition of equivalence you want your laws to use. And for many classes, those laws are using an equivalence not definable using Eq. A good example Is monad! You can not define Eq instances for arbitrary a-> m b. We can still define and talk about lawful monads.
Point of order though, no Num a instance has a Monoid a instance. Instead Sum a and Product a are the Monoid Instances.
Its approximately transitive. Distances always obey the triangle Inequality. Good enough for geometry. Also the emphasis is on the geometry / distance.