Hi All,
My first post here, so hello all and thanks for all the great work on Elixir so far!
This is a relatively simple proposal. Many of the Float functions already take precisions and, floating point arithmetic being what it is a comparison function that takes some sort of epsilon value is almost required for a lot of floating point work.
I would propose a spec something like:
@spec nearly_equal?(float, float, precision) :: bool
Using an epsilon value instead of a precision might make more sense in some contexts but since the rest of the functions in Float use a precision value that is probably more consistent.
Arguments for:
The documentation for Float already links to the above article discussing why such a function might be needed. Adding an implementation probably makes sense.
It is a sufficiently widely used function that a default implementation in the standard library is not going to be clutter.
It is a tricky function to get right and providing a default implementation would be beneficial.
Having it available might make devs more likely to use it instead of comparison or pattern matching floats.
Arguments against:
The function can already be written using the tools provided in the core language. Implementation of such a function is not necessary and arguably is something domain specific that should be written per project.
It doesn't behave very well with pattern matching. This could of course be rectified by making it usable as a function clause ala:
def float_stuff(x, y) when nearly_equal?(x, y, 5), do ...
There may be a copyright issue with just writing a version of the code linked.
Alternatives:
Using the same spec but calling the function equal? and defaulting precision to 15 might make more sense.