-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
math: add MaxUint, MinInt, MaxInt #28538
Comments
Isn't this platform dependent, not independent? |
It will give the correct max and min values for int and uint on 32 and 64 bit platforms. I don't have access to 8 and 16 bit ones but it should work there as well due to how it's calculated. |
In your specific implementation, you have them as typed constants instead of untyped. That's probably not preferable as all other constants defined for limits are untyped constants. implementation using |
Also, while you're at it, might be a good idea to add a max uintptr as well. Pretty sure the following is the only way to do it that results in an untyped constant:
Also, probably a good idea to get rid of |
I realized there may be a licensing issue with my implementation that doesn't use The implementation that does use unsafe and the uintptr however, I made myself and is all good to use. cc @go101 |
No need to worry about licensing issues for such a short purely-functional expression. |
I played around a bit with the changes @deanveloper proposed, especially the removal of the typing in the constants. Having the constants typed has actually one big advantage: The constant won't overflow due to incorrect type detection/resolution. https://play.golang.org/p/_iFMKqyauP9 When it is typed it should also protect against type narrowing. |
This is correct. Pretty sure there is an issue to make the
or another example
Although arguably it would be better to need to manually convert the constants to |
I always thought these were already part of the |
We already have math.MaxInt32 etc for all the sized types. Adding the unsized ones seems fine. It's a little weird that they're not there. @griesemer points out that in math/bits we have a bunch of sized functions but decided to include unsized ones too. Sure, we can add Int and Uint variants. Note that there is no math.MinUint8 etc so there should be no math.MinUint (it's always zero). I'd want to see a compelling reason for MaxUintptr though. Let's leave that one out. |
can add math.AbsInt()/AvgInt() as well, even better a whole wrapper as equivalent to float type. We found usually ints are more used than floats. Appreciate a varargs implementation of min/max/avg as well :) |
@rverma-jm This proposal is specifically about the functions mentioned in the title. Please do not hijack this proposal as it is now accepted. Instead open a new proposal if you want to add something else. Thanks. |
Change https://golang.org/cl/247058 mentions this issue: |
Change https://golang.org/cl/317911 mentions this issue: |
Documents the newly introduced: * MaxInt * MinInt * MaxUint Updates #28538. For #44513. Fixes #46012. Change-Id: Iab6bbcf8f76ebe105b973d5fd39b86b8cd078348 Reviewed-on: https://go-review.googlesource.com/c/go/+/317911 Trust: Heschi Kreinick <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]>
The math package is lacking
MaxInt
,MaxUint
,MinInt
andMinUint
.This makes writing programs that assign a max-value to int or uint without manually specifying the value impossible, if you want to run the program on both 32 and 64 bit platforms.
I have written an implementation of the constants mentioned above here: #28537
The text was updated successfully, but these errors were encountered: