Python has a pretty nice PEP for this https://www.python.org/dev/peps/pep-0485/
Allen Madsen http://www.allenmadsen.com On Thu, May 20, 2021 at 3:31 AM José Valim <[email protected]> wrote: > Thanks Eamonn for sharing! The article and its references led to very > interesting reading. > > In principle I don't see an issue with adding such a function, but I > believe we need to survey other languages and see what they are calling it > and which edge cases they consider too. > > Using the code above may be ok at the end, it is under Creative Commons, > but we need to survey around before. :) > > On Thu, May 20, 2021 at 12:21 AM Eamonn O'Brien <[email protected]> > wrote: > >> 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. >> >> As https://floating-point-gui.de/errors/comparison/ outlines writing >> such a function is both tricky and error prone so adding an implementation >> of that function to the core library makes sense. >> >> 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. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "elixir-lang-core" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/elixir-lang-core/23f8e3a6-0aa6-426c-bb67-497a6bd7bf66n%40googlegroups.com >> <https://groups.google.com/d/msgid/elixir-lang-core/23f8e3a6-0aa6-426c-bb67-497a6bd7bf66n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- > You received this message because you are subscribed to the Google Groups > "elixir-lang-core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2Bt6Qj6HnR9%2BvmGjAnLFwe4hjsYJgdwXWYb9njUirWggg%40mail.gmail.com > <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2Bt6Qj6HnR9%2BvmGjAnLFwe4hjsYJgdwXWYb9njUirWggg%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAK-y3CvTxMwB4bJT3w--7MYCyZ7VaNu8FNBuM_LMztFTZe0oOg%40mail.gmail.com.
