<div dir="ltr">Hmm, minefield ahead.. But surely there must be a "correct" compromise? (Even with a huge performance penalty.)<div><br></div><div>I'll just add that rwbarton had this comment earlier:</div><div><br></div><div>"<span style="color:rgb(0,0,0);font-family:Verdana,Arial,'Bitstream Vera Sans',Helvetica,sans-serif;font-size:13px">Be aware (if you aren't already) that GHC does not do any management of floating-point control registers, so functions called through FFI should take care to clean up their floating-point state, otherwise the rounding mode can change unpredictably at the level of Haskell code."<br></span></div><div><span style="color:rgb(0,0,0);font-family:Verdana,Arial,'Bitstream Vera Sans',Helvetica,sans-serif;font-size:13px"><br></span></div><div>So, there're some FFI related issues even if we just leave the work to C.</div><div><br></div><div>I'll also note that the current implementation of arithmetic on Double/Floats already has rounding mode issues: If someone does an FFI call to change the rounding mode via C (fgetround/fsetround functions) inside some IO block, then the arithmetic in that block cannot be "lifted" out even though it appears pure to GHC. Perhaps that should be filed as a bug too.</div><div><br></div><div>-Levent.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, May 5, 2015 at 8:07 AM, Carter Schonwald <span dir="ltr"><<a href="mailto:carter.schonwald@gmail.com" target="_blank">carter.schonwald@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Irk. If libm is busted when changing rounding modes, that puts a nasty twist on things. <div><br></div><div>I do agree that even if that hurdle is jumped, setting the local rounding mode will have to be part of every green thread context switch.  But if libm is hosed that kinda makes adding that machinery a smudge pointless until there's a good story for that. <div><div class="h5"><span></span><br><br>On Tuesday, May 5, 2015, Edward Kmett <<a href="mailto:ekmett@gmail.com" target="_blank">ekmett@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, May 5, 2015 at 7:22 AM, Scott Turner <span dir="ltr"><<a>2haskell@pkturner.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span>On 2015-05-05 00:54, Levent Erkok wrote:<br>
> I can see at least two designs:<br>
><br>
>    * One where the rounding mode goes with the operation: `fpAdd<br>
> RoundNearestTiesToEven 2.5 6.4`. This is the "cleanest" and the<br>
> functional solution, but could get quite verbose; and might be costly<br>
> if the implementation changes the rounding-mode at every issue.<br>
><br>
>    * The other is where the operations simply assume the<br>
> RoundNearestTiesToEven, but we have lifted IO versions that can be<br>
> modified with a "with" like construct:  `withRoundingMode<br>
> RoundTowardsPositive $ fpAddRM 2.5 6.4`. Note that `fpAddRM` (*not*<br>
> `fpAdd` as before) will have to return some sort of a monadic value<br>
> (probably in the IO monad) since it'll need to access the rounding<br>
> mode currently active.<br>
><br>
> Neither choice jumps out at me as the best one; and a hybrid might<br>
> also be possible. I'd love to hear any insight you gain regarding<br>
> rounding-modes during your experiment.<br>
<br>
</span>The monadic alternative is more readily extensible to handle IEEE 754's<br>
sticky flags: inexact, overflow, underflow, divide-by-zero, and invalid.<br></blockquote><div><br></div><div>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.</div><div><br></div><div>This was what I learned when I tried my own hand at it and failed:</div><div><br></div><div><a href="http://hackage.haskell.org/package/rounding" target="_blank">http://hackage.haskell.org/package/rounding</a><br></div><div><br></div><div>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.</div><div><br></div><div>Workarounds such as using <a href="http://lipforge.ens-lyon.fr/www/crlibm/" target="_blank">crlibm</a> exist, but isn't installed on most platforms and it would rather dramatically complicate the installation of ghc to incur the dependency.</div><div><br></div><div>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.)</div><div><br></div><div>-Edward</div><div><br></div><div><br></div><div><br></div><div>-Edward</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div><div>_______________________________________________<br>
Libraries mailing list<br>
<a>Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
</div></div></blockquote></div><br></div></div>
</blockquote></div></div></div>
<br>_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
<br></blockquote></div><br></div>