[go] math: implement Abs function using generics

105 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Sep 28, 2022, 8:33:18 PM9/28/22
to goph...@pubsubhelper.golang.org, Felipe Alves, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

math: implement Abs function using generics

Enabling the following list of numeric types:
- ~int,
- ~int8,
- ~int16,
- ~int32,
- ~int64,
- ~float32,
- ~float64

The existing implementation for Abs is optimized for float64 arguments.
This commit adds generic parameter to the Abs function after verifying
that it does not degrade performance

The investigation for this change is described at
https://github.com/golang/go/issues/55929

Fixes #55929

Change-Id: Id8824a5dbddc97a8f3669c1676961214c4622772
GitHub-Last-Rev: 6a152504797828bf6939102f7d4d3f21a768e816
GitHub-Pull-Request: golang/go#55931
---
M src/math/abs.go
M src/math/example_test.go
2 files changed, 59 insertions(+), 8 deletions(-)

diff --git a/src/math/abs.go b/src/math/abs.go
index 08be145..a314d64 100644
--- a/src/math/abs.go
+++ b/src/math/abs.go
@@ -10,6 +10,10 @@
//
// Abs(±Inf) = +Inf
// Abs(NaN) = NaN
-func Abs(x float64) float64 {
- return Float64frombits(Float64bits(x) &^ (1 << 63))
+type number interface {
+ ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64
+}
+
+func Abs[T number](x T) T {
+ return T(Float64frombits(Float64bits(float64(x)) &^ (1 << 63)))
}
diff --git a/src/math/example_test.go b/src/math/example_test.go
index a26d8cb..621d7d6 100644
--- a/src/math/example_test.go
+++ b/src/math/example_test.go
@@ -174,14 +174,32 @@
}

func ExampleAbs() {
- x := math.Abs(-2)
- fmt.Printf("%.1f\n", x)
+ xFloat64 := math.Abs(float64(-2))
+ fmt.Printf("%.1f, %T\n", xFloat64, xFloat64)

- y := math.Abs(2)
- fmt.Printf("%.1f\n", y)
+ yFloat64 := math.Abs(float64(2))
+ fmt.Printf("%.1f, %T\n", yFloat64, yFloat64)
+
+ xInt := math.Abs(int(-2))
+ fmt.Printf("%d, %T\n", xInt, xInt)
+
+ yInt := math.Abs(int(2))
+ fmt.Printf("%d, %T\n", yInt, yInt)
+
+ type int64Type int64
+ xTypeInt64 := math.Abs(int64Type(-2))
+ fmt.Printf("%d, %T\n", xTypeInt64, xTypeInt64)
+
+ yTypeInt64 := math.Abs(int64Type(2))
+ fmt.Printf("%d, %T\n", yTypeInt64, yTypeInt64)
+
// Output:
- // 2.0
- // 2.0
+ // 2.0, float64
+ // 2.0, float64
+ // 2, int
+ // 2, int
+ // 2, math_test.int64Type
+ // 2, math_test.int64Type
}
func ExampleDim() {
fmt.Printf("%.2f\n", math.Dim(4, -2))

To view, visit change 436296. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Id8824a5dbddc97a8f3669c1676961214c4622772
Gerrit-Change-Number: 436296
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Felipe Alves <felipe...@gmail.com>
Gerrit-MessageType: newchange

Gopher Robot (Gerrit)

unread,
Sep 28, 2022, 8:39:12 PM9/28/22
to Gerrit Bot, Felipe Alves, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.

View Change

    To view, visit change 436296. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Id8824a5dbddc97a8f3669c1676961214c4622772
    Gerrit-Change-Number: 436296
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Felipe Alves <felipe...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Thu, 29 Sep 2022 00:39:08 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Rob Pike (Gerrit)

    unread,
    Sep 28, 2022, 10:08:10 PM9/28/22
    to Gerrit Bot, Felipe Alves, goph...@pubsubhelper.golang.org, Rob Pike, Robert Griesemer, Russ Cox, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Robert Griesemer, Russ Cox.

    Patch set 1:Hold +1

    View Change

      To view, visit change 436296. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Id8824a5dbddc97a8f3669c1676961214c4622772
      Gerrit-Change-Number: 436296
      Gerrit-PatchSet: 1
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Rob Pike <r...@golang.org>
      Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
      Gerrit-Reviewer: Russ Cox <r...@golang.org>
      Gerrit-CC: Felipe Alves <felipe...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Robert Griesemer <g...@golang.org>
      Gerrit-Attention: Russ Cox <r...@golang.org>
      Gerrit-Comment-Date: Thu, 29 Sep 2022 02:08:04 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      Gerrit-MessageType: comment

      Rob Pike (Gerrit)

      unread,
      Sep 29, 2022, 8:04:00 PM9/29/22
      to Gerrit Bot, Felipe Alves, goph...@pubsubhelper.golang.org, Rob Pike, Robert Griesemer, Russ Cox, Gopher Robot, golang-co...@googlegroups.com

      Attention is currently required from: Robert Griesemer, Russ Cox.

      View Change

      1 comment:

      • Patchset:

        • Patch Set #1:

          The proposal was declined, so this will not happen as written here.

      To view, visit change 436296. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Id8824a5dbddc97a8f3669c1676961214c4622772
      Gerrit-Change-Number: 436296
      Gerrit-PatchSet: 1
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Rob Pike <r...@golang.org>
      Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
      Gerrit-Reviewer: Russ Cox <r...@golang.org>
      Gerrit-CC: Felipe Alves <felipe...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Robert Griesemer <g...@golang.org>
      Gerrit-Attention: Russ Cox <r...@golang.org>
      Gerrit-Comment-Date: Fri, 30 Sep 2022 00:03:53 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Gerrit-MessageType: comment

      Gopher Robot (Gerrit)

      unread,
      Aug 10, 2024, 5:57:19 PM8/10/24
      to Gerrit Bot, Felipe Alves, goph...@pubsubhelper.golang.org, Rob Pike, Robert Griesemer, Russ Cox, golang-co...@googlegroups.com

      Gopher Robot abandoned this change

      Related details

      Attention set is empty
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Holds
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: abandon
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages