Proposal: Add "fma" to the RealFloat class

Edward Kmett ekmett at
Tue May 5 14:51:41 UTC 2015

On Tue, May 5, 2015 at 7:22 AM, Scott Turner <2haskell at> wrote:

> On 2015-05-05 00:54, Levent Erkok wrote:
> > I can see at least two designs:
> >
> >    * One where the rounding mode goes with the operation: `fpAdd
> > RoundNearestTiesToEven 2.5 6.4`. This is the "cleanest" and the
> > functional solution, but could get quite verbose; and might be costly
> > if the implementation changes the rounding-mode at every issue.
> >
> >    * The other is where the operations simply assume the
> > RoundNearestTiesToEven, but we have lifted IO versions that can be
> > modified with a "with" like construct:  `withRoundingMode
> > RoundTowardsPositive $ fpAddRM 2.5 6.4`. Note that `fpAddRM` (*not*
> > `fpAdd` as before) will have to return some sort of a monadic value
> > (probably in the IO monad) since it'll need to access the rounding
> > mode currently active.
> >
> > Neither choice jumps out at me as the best one; and a hybrid might
> > also be possible. I'd love to hear any insight you gain regarding
> > rounding-modes during your experiment.
> The monadic alternative is more readily extensible to handle IEEE 754's
> sticky flags: inexact, overflow, underflow, divide-by-zero, and invalid.

This gets messier than you'd think. Keep in mind we switch contexts within
our own green threads constantly on shared system threads / capabilities so
the current rounding mode, sticky flags, etc. would become something you'd
have to hold per Thread, and then change proactively as threads migrate
between CPUs / capabilities, which we're basically completely unaware of
right now.

This was what I learned when I tried my own hand at it and failed:

There found I gave up, and moved setting the rounding mode into custom
primitives themselves. But even then you find other problems! The libm
versions of almost every combinator doesn't just give slightly wrong
answers when you switch rounding modes, it gives _completely_ wrong answers
when you switch rounding modes. cos basically starts looking like a random
number generator. This is rather amusing given that libm is the library
that specified how to change the damn rounding mode and fixing this fact it
was blocked by Ulrich Drepper when I last looked.

Workarounds such as using crlibm <>
exist, but isn't installed on most platforms and it would rather
dramatically complicate the installation of ghc to incur the dependency.

This is why I've switched to using MPFR for anything with known rounding
modes and just paying a pretty big performance tax for correctness. (That
and I'm working to release a library that does exact real arithmetic using
trees of nested linear fractional transformations -- assuming I can figure
out how to keep performance high enough.)



> _______________________________________________
> Libraries mailing list
> Libraries at
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Libraries mailing list