Go 中 int 类型的最大值
如何指定无符号整数类型可表示的最大值?
我想知道如何在下面的循环中初始化 min
,该循环迭代地计算某些结构的最小和最大长度。
var minLen uint = ???
var maxLen uint = 0
for _, thing := range sliceOfThings {
if minLen > thing.n { minLen = thing.n }
if maxLen < thing.n { maxLen = thing.n }
}
if minLen > maxLen {
// If there are no values, clamp min at 0 so that min <= max.
minLen = 0
}
这样第一次比较时,minLen >= n
。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
https://groups.google.com/group/golang-nuts /msg/71c307e4d73024ce?pli=1
相关部分:
根据 @CarelZA 的评论:
https://groups.google.com/group/golang-nuts/msg/71c307e4d73024ce?pli=1
The germane part:
As per @CarelZA's comment:
https://golang.org/ref/spec#Numeric_types 了解物理类型限制。
最大值在 math 包中定义,因此在您的情况下: math.MaxUint32
小心,因为没有溢出 - 增加超过最大值会导致环绕。
https://golang.org/ref/spec#Numeric_types for physical type limits.
The max values are defined in the math package so in your case: math.MaxUint32
Watch out as there is no overflow - incrementing past max causes wraparound.
我将使用 math 包来获取整数的最大值和最小值:
输出:
I would use
math
package for getting the maximal and minimal values for integers:Output:
注意:此答案自 go 1.17 起被取代,其中包括 e8eb1d8;即:
math
包现在包含math.MaxUint
、math.MaxInt
和math.MinInt
常量。简单总结:
背景:
我想您知道,
uint
类型的大小与uint32
或uint64
相同,具体取决于在平台上你在。通常,只有在不存在接近最大值的风险时,人们才会使用这些的未调整大小的版本,因为没有大小规范的版本可以使用“本机”类型,具体取决于平台,这往往会更快。请注意,它往往“更快”,因为使用非本机类型有时需要处理器执行额外的数学和边界检查,以便模拟更大或更小的整数。考虑到这一点,请注意处理器(或编译器的优化代码)的性能几乎总是比添加您自己的边界检查代码更好,因此如果存在任何发挥作用的风险,可能会导致明智的做法是简单地使用固定大小的版本,并让优化的仿真处理由此产生的任何后果。
话虽如此,在某些情况下,了解您正在处理的内容仍然很有用。
包“math/bits”包含
uint< 的大小/code>,以位为单位。要确定最大值,请将
1
移动相应位数,再减 1。即:(1 << bits.UintSize) - 1
请注意,计算最大值时
uint
的值,通常需要将其显式放入uint
(或更大)变量中,否则编译器可能会失败,因为它会默认尝试分配将该计算转化为有符号的int
(显然,它不适合),所以:这是您问题的直接答案,但还有一些您可能感兴趣的相关计算。
根据spec、
uint
和int
始终相同的尺寸。所以我们也可以使用此常量来确定
int
的最大值,方法是采用相同的答案并除以2
,然后减去1
。即:(1 << bits.UintSize) / 2 - 1
以及
int
的最小值,通过将1
移位那么多位并将结果除以-2
。即:(1 <
总结:
MaxUint:
(1 <
MaxInt:
(1 << bits.UintSize) / 2 - 1
MinInt:
(1 <
完整示例 (应该和下面一样)
note: this answer is superseded as of go 1.17, which included e8eb1d8; ie: the
math
package now includes constants formath.MaxUint
,math.MaxInt
, andmath.MinInt
.Quick summary:
Background:
As I presume you know, the
uint
type is the same size as eitheruint32
oruint64
, depending on the platform you're on. Usually, one would use the unsized version of these only when there is no risk of coming close to the maximum value, as the version without a size specification can use the "native" type, depending on platform, which tends to be faster.Note that it tends to be "faster" because using a non-native type sometimes requires additional math and bounds-checking to be performed by the processor, in order to emulate the larger or smaller integer. With that in mind, be aware that the performance of the processor (or compiler's optimised code) is almost always going to be better than adding your own bounds-checking code, so if there is any risk of it coming into play, it may make sense to simply use the fixed-size version, and let the optimised emulation handle any fallout from that.
With that having been said, there are still some situations where it is useful to know what you're working with.
The package "math/bits" contains the size of
uint
, in bits. To determine the maximum value, shift1
by that many bits, minus 1. ie:(1 << bits.UintSize) - 1
Note that when calculating the maximum value of
uint
, you'll generally need to put it explicitly into auint
(or larger) variable, otherwise the compiler may fail, as it will default to attempting to assign that calculation into a signedint
(where, as should be obvious, it would not fit), so:That's the direct answer to your question, but there are also a couple of related calculations you may be interested in.
According to the spec,
uint
andint
are always the same size.So we can also use this constant to determine the maximum value of
int
, by taking that same answer and dividing by2
then subtracting1
. ie:(1 << bits.UintSize) / 2 - 1
And the minimum value of
int
, by shifting1
by that many bits and dividing the result by-2
. ie:(1 << bits.UintSize) / -2
In summary:
MaxUint:
(1 << bits.UintSize) - 1
MaxInt:
(1 << bits.UintSize) / 2 - 1
MinInt:
(1 << bits.UintSize) / -2
full example (should be the same as below)
我最初使用的是从 @nmichaels 在他的答案中使用的讨论线程中获取的代码。我现在使用稍微不同的计算。我已经添加了一些评论,以防其他人有与 @Arijoon 相同的查询。
最后两个步骤之所以有效,是因为正数和负数在二进制补码算术中的表示方式。 数字类型 上的 Go 语言规范部分将读者引向相关的维基百科文章。我还没读过,但我确实从 Charles Petzold 的 Code by Charles Petzold 书中了解了二进制补码,这是对计算机和编码基础知识的简单易懂的介绍。
我将上面的代码(减去大部分注释)放入一个小整数数学包中。
I originally used the code taken from the discussion thread that @nmichaels used in his answer. I now use a slightly different calculation. I've included some comments in case anyone else has the same query as @Arijoon
The last two steps work because of how positive and negative numbers are represented in two's complement arithmetic. The Go language specification section on Numeric types refers the reader to the relevant Wikipedia article. I haven't read that, but I did learn about two's complement from the book Code by Charles Petzold, which is a very accessible intro to the fundamentals of computers and coding.
I put the code above (minus most of the comments) in to a little integer math package.
Go-1.17 现在在 数学 包。
测试上面的代码:https://play.golang.org/p/5R2iPasn6OZ
来自 Go 1.17 发行说明:https://golang.org/doc/go1.17#math
另请参阅Go 源代码: https://github.com/ golang/go/blob/master/src/math/const.go#L39
Go-1.17 now defines
MaxUint
,MaxInt
andMinInt
constants in the math package.Test this above code: https://play.golang.org/p/5R2iPasn6OZ
From the Go 1.17 Release Notes: https://golang.org/doc/go1.17#math
See also the Go source code: https://github.com/golang/go/blob/master/src/math/const.go#L39
来自数学库: https://github.com/golang /go/blob/master/src/math/const.go#L39
From math lib: https://github.com/golang/go/blob/master/src/math/const.go#L39
使用数学包中定义的常量:
Use the constants defined in the math package:
解决此问题的一种方法是从值本身获取起点:
One way to solve this problem is to get the starting points from the values themselves:
Go 1.17(2021 年第 4 季度)可能会有所帮助,提交 e8eb1d8,如 Go101:
这修复了 问题 28538 由
Silentd00m
,由 CL 247058。测试说明了其工作原理:
Go 1.17 (Q4 2021) might help, with commit e8eb1d8, as noted by Go101:
That fixes issue 28538 reported by
Silentd00m
, reviewed with CL 247058.Tests are illustrating how this works:
我一直记得的方式是,你取位(
int8
是 8 位,int
是32 位),除以 8 即可得到字节(
int8
是一个字节,int
将是四个字节)。
每个字节都是
0xFF
(有符号整数除外,在这种情况下最高有效字节)字节将为
0x7F
)。这是结果:The way I always remember it, is you take the bits (
int8
is 8 bits,int
is32 bits), divide by 8 and you get the bytes (
int8
would be one byte,int
would be four bytes).
Each byte is
0xFF
(except for signed integer, in which case most significantbyte will be
0x7F
). Here is result:一个轻量级的 package 包含它们(以及其他 int 类型限制和一些广泛使用的整数)功能):
A lightweight package contains them (as well as other int types limits and some widely used integer functions):