[Haskell-cafe] Sum and Product do not respect the Monoid laws

Carter Schonwald carter.schonwald at gmail.com
Fri Sep 19 17:26:13 UTC 2014


Indeed,

Also Floating point numbers are *NOT* real numbers, they are approximate
points on the real line that we pretend are exact reals but really are a
very different geometry all together! :)

If you want to get extra precision in a floating point computation in a way
avoids the "discrepancy" when permuting the numbers, the compensated
library by Edward Kmett http://hackage.haskell.org/package/compensated lets
you easily double or quadruple the number of bits of precision you get in
sums and products, which makes a lot of these problems go way quite nicely!

Floats and Doubles are not exact numbers, dont use them when you expect
things to behave "exact". NB that even if you have *exact* numbers, the
exact same precision issues will still apply to pretty much any computation
thats interesting (ignoring what the definition of interesting is).  Try
defining things like \ x -> SquareRoot x or \x-> e^x  on the rational
numbers! Questions of precision still creep in!

So I guess phrased another way, a lot of the confusion / challenge in
writing floating point programs lies in understanding the representation,
its limits, and the ways in which it will implicitly manage that precision
tracking book keeping for you.

Computational mathematics is a really rich and complicated topic! Theres
many more (valid and different) constructive models of different classes of
numbers than you'd expect!


On Fri, Sep 19, 2014 at 7:31 AM, David Thomas <davidleothomas at gmail.com>
wrote:

> Restricting to Integral only would be overconstraining - Sum Rational
> is perfectly well behaved as a monoid.  I could go either way on
> whether Double and Float should be excluded - I'm actually slightly
> sympathetic to the notion that they shouldn't even be Num.
>
> On Fri, Sep 19, 2014 at 6:58 AM, Jason Choy <jjwchoy at gmail.com> wrote:
> > The Monoid instances for Sum and Product do not respect the Monoid laws.
> >
> > The instances are:
> >
> > Num a => instance Monoid (Sum a)
> > Num a => instance Monoid (Product a)
> >
> > Float and Double are instances of the Num typeclass, however, floating
> point
> > addition and multiplication are not associative. Here's an example:
> >
> >> (Sum 1234.567 `mappend` Sum 45.67834) `mappend` Sum 0.0004
> > Sum {getSum = 1280.2457399999998}
> >
> >> Sum 1234.567 `mappend` (Sum 45.67834 `mappend` Sum 0.0004)
> > Sum {getSum = 1280.24574}
> >
> > Shouldn't these instances be constrained to Integral types?
> >
> > Jason
> >
> >
> > _______________________________________________
> > Haskell-Cafe mailing list
> > Haskell-Cafe at haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140919/b37de751/attachment.html>


More information about the Haskell-Cafe mailing list