Skip to content
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

proposal: builtin: add abs to get absolute value of numbers( integer and float pointers) #60623

Closed
cuiweixie opened this issue Jun 6, 2023 · 12 comments
Labels
Milestone

Comments

@cuiweixie
Copy link
Contributor

cuiweixie commented Jun 6, 2023

I proposal to add builtin functions abs.

for integer, it's logic like:

import (
	"unsafe"
)

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

func abs[T Integer](a T) T {
	mask := a >> (unsafe.Sizeof(a)*8 - 1)
	return (a + mask) ^ mask
}

for float pointer it acts like math.Abs.

@gopherbot gopherbot added this to the Proposal milestone Jun 6, 2023
@cuiweixie cuiweixie changed the title proposal: math: add Abs proposal: math: add FastAbs Jun 6, 2023
@hopehook
Copy link
Member

hopehook commented Jun 6, 2023

I thought it might be more convenient to add an abs to the built-in function to solve the problem of taking the absolute value of all integers and floating point numbers.

@cuiweixie
Copy link
Contributor Author

I thought it might be more convenient to add an abs to the built-in function to solve the problem of taking the absolute value of all integers and floating point numbers.

I think it's a good idea

@ianlancetaylor
Copy link
Contributor

Note that you can write this function as max(x, -x).

If we do want to add this somewhere, I don't think the math package is the right place, as the math package is primarily for floating-point operations. And I definitely don't think that FastAbs is the right name; it's no faster than math.Abs.

@cuiweixie cuiweixie changed the title proposal: math: add FastAbs proposal: builtin: add abs to get absolute value of numbers( integer and float pointers) Jun 7, 2023
@cuiweixie
Copy link
Contributor Author

Note that you can write this function as max(x, -x).

If we do want to add this somewhere, I don't think the math package is the right place, as the math package is primarily for floating-point operations. And I definitely don't think that FastAbs is the right name; it's no faster than math.Abs.

How about add a abs function as builtin. it calculate the absolute value of both integers and float pointer numbers.

@rsc
Copy link
Contributor

rsc commented Jun 28, 2023

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Jul 5, 2023

If we were going to add a builtin abs, I would make it take a signed int and return the corresponding unsigned type, so that there are no cases where it can return a negative number. func Abs(int8)int8 cannot do anything useful with Abs(-128) for example.

That said, abs comes up far less often than min and max and can easily be provided by third-party libraries for now.

@rsc
Copy link
Contributor

rsc commented Jul 12, 2023

Based on the discussion above, this proposal seems like a likely decline.
— rsc for the proposal review group

@andig
Copy link
Contributor

andig commented Jul 12, 2023

Note that you can write this function as max(x, -x).

How would that work on an int8 for -128?

@zephyrtronium
Copy link
Contributor

In int8, -(-128) is -128, so it would evaluate max(-128, -128) = -128.

@fzipp
Copy link
Contributor

fzipp commented Jul 12, 2023

In int8, -(-128) is -128, so it would evaluate max(-128, -128) = -128.

Same as the proposed abs function.

@rsc
Copy link
Contributor

rsc commented Jul 19, 2023

No change in consensus, so declined.
— rsc for the proposal review group

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

No branches or pull requests

9 participants