Gerrit Bot has uploaded this change for review.
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.
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.
Attention is currently required from: Robert Griesemer, Russ Cox.
Patch set 1:Hold +1
Attention is currently required from: Robert Griesemer, Russ Cox.
1 comment:
Patchset:
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.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |