Skip to content

math: use Abs rather than if x < 0 { x = -x } #21812

@mundaym

Description

@mundaym

Now that #13095 is closed we should consider using the Abs function where possible in the math library (and possibly elsewhere in the std lib).

For example, this statement in j0.go could presumably be replaced:

if x < 0 {
    x = -x
}

There are subtle differences between the if statement and Abs (NaN handling for example) but most of the math functions handle those cases explicitly anyway.

We should also be aware that don't fully optimize Abs on all architectures yet, but I think this will come with time. s390x and ppc64x, for example, can do Abs in a single instruction whereas the if statement is more difficult to optimize in the general case (because of NaN/-0.0 inputs mainly).

This could be a good issue for a first time contributor, though be aware that you should include before/after benchmarks and for some functions you may need to manually disable assembly implementations to exercise the Go code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions