-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: math: add Float, Integer, Complex, Number interfaces #61914
Comments
This has come up before, though without a definite answer. See #52427, which was closed with the suggestion to open a proposal specifically for this purpose. If I remember right, one of the main hesitations of doing this would be that packages that didn't need any other functionality from the Separately, I think that I'd want type Signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
type Unsigned interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
type Integer interface {
Signed | Unsigned
} |
If the issue was importing whole package because of these interfaces, then what about creating And regarding |
There's also #60274 that is for adding some helpers to math but notes that it could go in a package under math and if so that package would be a good place for the constraints. |
@djordje200179 Can you please give some (ideally real-world) examples of how and where this would be used? |
Those things were first ones that came to my mind, but there are probably a lot more things that could be done. |
Our experiences with x/exp/constraints was that approximately nobody used most of the constraints. So I suggest that we focus first on APIs that might want to use these constraints. Then if we those APIs prove popular, we can consider adding the constraints. After all, the constraints might prove to be useful but they are never necessary. We can go from uses to the constraints, rather than from the constraints to uses. |
The current math package is pretty much all float64. Only a handful of functions in it even make sense on integers. On a quick scan, the only ones I see are Abs, Dim, Pow, and Pow10. It might make more sense to create some new package that focuses on whatever generic integer math needs to be done, and putting constraints there. |
In my opinion, it would be okay to make Abs, Pow and Pow10 generic functions. We would avoid unnecessary casts between integer and float types. Also, other functions wouldn't need to "force" float64 types. Someone that uses float32 type for his purposes after calling math functions needs to downcast returned result from float64 to float32. Same thing with cmplx package, that "forces" use of complex128. |
While I agree with you and think it would be worth the cost, changing the signature of exported functions would break the backward-compatibility promise. |
I would like to express my support for generic float in math, because i often work with float32 and it makes the math package un-practicable for my use case. Also, curios how would it break the backward compatibility promise? |
@BieHDC Thank you for your support!
I have also thought about same thing, but @mohamedattahri said that changing the signature breaks code. So, I'm not sure now what is the truth. |
// If this is generic, this becomes an error because it
// would be a usage of an uninstantiated generic
// function.
f := math.Sin |
@DeedleFake Thank you for clarification! I have forgot about function type and having variables. |
Reference: golang/go#61914 It was only used for the `constraints` package, the type definitions are relatively trivial and static, and the Go language maintainers are not sure if/where these will live in the standard library, which implies (at least to me) they could also disappear from x/exp.
Reference: golang/go#61914 It was only used for the `constraints` package, the type definitions are relatively trivial and static, and the Go language maintainers are not sure if/where these will live in the standard library, which implies (at least to me) they could also disappear from x/exp.
In exp package there was
constraints
package that contained some interfaces, among whichOrdered
was added to Go 1.21.I suggest adding those other number-related interfaces from the packages to the
math
package. They can be helpful and useful, and I think thatmath
package is the right place for those interfaces (because they group numeric types).Code that should be added to the
math
package:The text was updated successfully, but these errors were encountered: