Skip to content

proposal: math: implement abs function using generic #71796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
streamdp opened this issue Feb 17, 2025 · 3 comments
Closed

proposal: math: implement abs function using generic #71796

streamdp opened this issue Feb 17, 2025 · 3 comments
Labels
Milestone

Comments

@streamdp
Copy link
Contributor

streamdp commented Feb 17, 2025

Proposal Details

Hello! Based on my experience using Go, there are certain inconveniences associated with the implementation of some functions from the math package. And if earlier it was clear that there were no changes in this package due to the fact that generic functionality was expected, now it looks like nothing prevents us from starting to replace the implementation of some functions. In particular, Abs(), which is often used in the practice of solving individual problems, for example, when learning a language or just try to get a star on "advent of code" =).

I found several issues related indirectly and directly to this problem:
proposal: math: add package math/ints for common operations on integers #41157
proposal: math: add Float, Integer, Complex, Number interfaces #61914

All of us know: we could implement someting we need ourselves, but it's really nice not having to think about implementing simple things.

I did a little research on the question and came up with something like this implementation:

func Abs[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

and even write a simple benchmark to compare this with current implementation in the math package:

goos: linux
goarch: amd64
pkg: proposal-abs
cpu: Intel(R) Core(TM) i3-7100U CPU @ 2.40GHz
BenchmarkAbsInt64-4                             213823255               56.11 ns/op
BenchmarkAbsFloat64-4                           211663220               57.91 ns/op
BenchmarkAbsCurrentImplementationFloat64-4      168030234               66.26 ns/op
PASS
ok      proposal-abs    35.468s

It's wokrs the same on my opinion on my platform. So, how about include something like that in the math package? Or am I missing something? And also it's possible to make this implementation (and changes like this) much prettier if include "golang.org/x/exp/constraints" in the builtin package.

@gopherbot gopherbot added this to the Proposal milestone Feb 17, 2025
@seankhliao
Copy link
Member

it's not backwards compatible #55929 #64871

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 17, 2025
@streamdp
Copy link
Contributor Author

it's not backwards compatible #55929 #64871

I think it's not a problem write something like that:

func AwesomeAbs[T ...](x T) T{
...
}

// Abs for backward compatibility
var Abs = AwesomeAbs[float64]

Looks weird, ofcourse, but if come up with a nice name it's workable and ready for use =)

@ianlancetaylor
Copy link
Contributor

Coming up with a new name is hard.

#48287

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants