Wrapped floating point type with embedded epsilon

I am working on some geometric calculations that will require me to compare coordinates based on pairs. I usually deal with floating point inaccuracies in this situation, including some artificial epsilon. This is common and there is a lot of information on this topic.

http://floating-point-gui.de/errors/comparison/

http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

My thought is to wrap Double in newtype and implement Eq and Ord with epsilon. It seems like such an obvious concept that either it has already been done, or it should be in the Hackage library, or something is clearly wrong with a concept that I haven't thought of yet. So my questions. Does anyone know of an existing module that contains a similar type (I did a quick search and didn't see anything)? Or is it a fake idea? Thank.

+3


source to share


1 answer


This is not a bogus idea. One approach is to create types that allow you to write floating point expressions that require some of the configuration data to evaluate, that is, the epsilon value. This will work the same way as the readers monad.

A good approach to this problem is given in:



http://okmij.org/ftp/Haskell/types.html#Prepose

and an efficient implementation for GHC can be found by cracking the reflection package.

+3


source







All Articles