golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.8k stars 17.64k forks source link

proposal: math: add package math/ints for common operations on integers #41157

Open markusheukelom opened 4 years ago

markusheukelom commented 4 years ago

The package math/ints would functions for common operations on integers:

Max(a, b int) int
Min(a, b int) int
Abs(value int) int
Pow(a int, b uint) int

Rationale: although Max, Min and Abs are trivial to implement, they are used so often they probably warrant being in the standard library.

The list above is not meant to be exhaustive, but more of a starting point. The package could also contain constants IntMax, IntMin, Int64Max, Int64Min.

martisch commented 4 years ago

I think if this is added to std library at all its better to wait for generics.

There have been previous proposals and discussions about this https://groups.google.com/forum/#!topic/golang-nuts/dbyqx_LGUxM but none was ever accepted.

Does this existing package suffice? https://github.com/pkg/math

markusheukelom commented 4 years ago

Thanks for the links. Both are useful, although it seems the discussion diverges quickly into generics. I can't really find the reason for why a separate package for these functions would be a bad idea.

I have my own package ints of course, so it doesn't really matter that much. It's just strange this is not present in the Go stdlib given its common use. It's weird when learning Go and still weird after two years of using it.

Having these function will probably also reduce the complaints about the lack of generics a bit.

ianlancetaylor commented 4 years ago

There is an active design draft for generics (https://go.googlesource.com/proposal/+/refs/heads/master/design/go2draft-type-parameters.md). Let's put this proposal on hold until we have either accepted or rejected that design.

earthboundkid commented 1 year ago

This can be taken off hold.

ianlancetaylor commented 1 year ago

Thanks, moved back to incoming.