It gives a different result for -0: https://go.dev/play/p/LAVIaAZsVNi Also, the math.Abs implementation is branch-less. Depending on what the compiler makes of either, that might perform better.
On Sat, 19 Oct 2024 at 16:28, Diego Augusto Molina < diegoaugustomol...@gmail.com> wrote: > Hi! I wanted to ask what is the difference between the current > implementation of math.Abs and the following: > > ```go > func Abs(x float64) float64 { > if x < 0 { > return -x > } > return x > } > ``` > > math.Abs would appear to return a copy of the input with the sign bit set > to zero. I tried the special conditions of NaN (the one returned by > math.NaN()), +Inf, and -Inf and the above would appear to work the same. > > I guess my main question is: would "-x" behave differently under any > condition? Was there a particular motivation for the current > implementation? Not judging nor asking for any change, just trying to learn. > > Thank you! > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/0cffdc6f-65c3-45c1-b003-ef3c27a0542en%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/0cffdc6f-65c3-45c1-b003-ef3c27a0542en%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfHANuTXzXuJC_7jS%3DyL4M9iY95H1ctXhueHQJyHt71-hg%40mail.gmail.com.