Newtype wrapper for Float/Double with sensible Eq/Ord instances

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Newtype wrapper for Float/Double with sensible Eq/Ord instances

Zemyla

There should be a newtype wrapper that wraps a RealFloat, keeps the Enum/Num/Fractional/so on instances, but gives it sensible ones for Eq and Ord, with the following order:

NaN < -Inf < negative < -0 < +0 < positive < +Inf

It may not belong in base, but if nothing else it definitely belongs in containers, because Set and Map depend critically on sensible instances of Ord.

(Also, it should newtype derive Read and Show, so that people using them with GHCi see "fromList [(NaN, yadda), (-1.5, yadda), (2.7, blah), (+Inf, blah)]" instead of "fromList [(FloatOrd { unwrapFloat :: NaN }, yadda), ...".)


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Newtype wrapper for Float/Double with sensible Eq/Ord instances

chessai .
I would argue there is no "sensible" implementation of Eq and Ord for floating point. Better to have PartialEq [1] /PartialOrd [2].


[2]: https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html

On Sat, Nov 30, 2019, 11:21 AM Zemyla <[hidden email]> wrote:

There should be a newtype wrapper that wraps a RealFloat, keeps the Enum/Num/Fractional/so on instances, but gives it sensible ones for Eq and Ord, with the following order:

NaN < -Inf < negative < -0 < +0 < positive < +Inf

It may not belong in base, but if nothing else it definitely belongs in containers, because Set and Map depend critically on sensible instances of Ord.

(Also, it should newtype derive Read and Show, so that people using them with GHCi see "fromList [(NaN, yadda), (-1.5, yadda), (2.7, blah), (+Inf, blah)]" instead of "fromList [(FloatOrd { unwrapFloat :: NaN }, yadda), ...".)

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Newtype wrapper for Float/Double with sensible Eq/Ord instances

Carter Schonwald
@chess, [hidden email] 
the ieee standard actually defines the following total order (i can send you the standard wrt the 2008 version if need be)
negative nans, -inf, negative finite, 0's , positive finite, inf, positive nans  (a very very different ordering than proposed here)

@Zem, i would love some collab getting some RTS support into ghc for signaling nans, been a bit too busy to do all the work myself!

so -1 on this proposal, and if you look back in the archives a few years ago you can see  a pretty lengthy discussion around a bit more of a fleshed out proposal,
though some of the end state work requires some more work on the ghc RTS 

the first piece needed to (subsequently implement the rest) appears in ghc 8.10, https://gitlab.haskell.org/ghc/ghc/commits/ghc-8.10.1-alpha1/compiler/cmm/CmmCallConv.hs 

it makes SSE2 the only floating point flavor on intel platform (no more x87), which simplifies a lot of 1) determinism in rounding issues that by default would plague 32bit intel platforms, and also cleaned up a pretty crufty corner of the NCG that created a lot of other spurious complexity in improving floating point stuff

On Sat, Nov 30, 2019 at 1:35 PM chessai . <[hidden email]> wrote:
I would argue there is no "sensible" implementation of Eq and Ord for floating point. Better to have PartialEq [1] /PartialOrd [2].


[2]: https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html

On Sat, Nov 30, 2019, 11:21 AM Zemyla <[hidden email]> wrote:

There should be a newtype wrapper that wraps a RealFloat, keeps the Enum/Num/Fractional/so on instances, but gives it sensible ones for Eq and Ord, with the following order:

NaN < -Inf < negative < -0 < +0 < positive < +Inf

It may not belong in base, but if nothing else it definitely belongs in containers, because Set and Map depend critically on sensible instances of Ord.

(Also, it should newtype derive Read and Show, so that people using them with GHCi see "fromList [(NaN, yadda), (-1.5, yadda), (2.7, blah), (+Inf, blah)]" instead of "fromList [(FloatOrd { unwrapFloat :: NaN }, yadda), ...".)

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Newtype wrapper for Float/Double with sensible Eq/Ord instances

Carter Schonwald
big picture: improving floating point and friends is +1, and we can definitely have good quality newtypes, but lets make sure they're going to make everyone happy! 

(one interesting question, what about  playing wiht newtypes that use reflection tricks to embed a relative error distance tolerance for equality?)

On Mon, Dec 2, 2019 at 1:35 AM Carter Schonwald <[hidden email]> wrote:
@chess, [hidden email] 
the ieee standard actually defines the following total order (i can send you the standard wrt the 2008 version if need be)
negative nans, -inf, negative finite, 0's , positive finite, inf, positive nans  (a very very different ordering than proposed here)

@Zem, i would love some collab getting some RTS support into ghc for signaling nans, been a bit too busy to do all the work myself!

so -1 on this proposal, and if you look back in the archives a few years ago you can see  a pretty lengthy discussion around a bit more of a fleshed out proposal,
though some of the end state work requires some more work on the ghc RTS 

the first piece needed to (subsequently implement the rest) appears in ghc 8.10, https://gitlab.haskell.org/ghc/ghc/commits/ghc-8.10.1-alpha1/compiler/cmm/CmmCallConv.hs 

it makes SSE2 the only floating point flavor on intel platform (no more x87), which simplifies a lot of 1) determinism in rounding issues that by default would plague 32bit intel platforms, and also cleaned up a pretty crufty corner of the NCG that created a lot of other spurious complexity in improving floating point stuff

On Sat, Nov 30, 2019 at 1:35 PM chessai . <[hidden email]> wrote:
I would argue there is no "sensible" implementation of Eq and Ord for floating point. Better to have PartialEq [1] /PartialOrd [2].


[2]: https://doc.rust-lang.org/std/cmp/trait.PartialOrd.html

On Sat, Nov 30, 2019, 11:21 AM Zemyla <[hidden email]> wrote:

There should be a newtype wrapper that wraps a RealFloat, keeps the Enum/Num/Fractional/so on instances, but gives it sensible ones for Eq and Ord, with the following order:

NaN < -Inf < negative < -0 < +0 < positive < +Inf

It may not belong in base, but if nothing else it definitely belongs in containers, because Set and Map depend critically on sensible instances of Ord.

(Also, it should newtype derive Read and Show, so that people using them with GHCi see "fromList [(NaN, yadda), (-1.5, yadda), (2.7, blah), (+Inf, blah)]" instead of "fromList [(FloatOrd { unwrapFloat :: NaN }, yadda), ...".)

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries