Norio,
The math package does not contain constants for the int and uint
types; they are implementation defined, and are either 32 or 64 bits.
http://golang.org/doc/go_spec.html#Numeric_types
Since integer types use two's complement arithmetic, you can infer the
min/max constant values for int and uint. For example,
const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(^uint(0) >> 1)
const MinInt = -(MaxInt - 1)
// Note the use of << to create an untyped constant. |
const bitsPerWord = 32 << uint(^uint(0)>>63) |
// Implementation-specific size of int and uint in bits. |
const BitsPerWord = bitsPerWord // either 32 or 64 |
// Implementation-specific integer limit values. |
const ( |
MaxInt = 1<<(BitsPerWord-1) - 1 // either 1<<31 - 1 or 1<<63 - 1 |
MinInt = -MaxInt - 1 // either -1 << 31 or -1 << 63 |
MaxUint = 1<<BitsPerWord - 1 // either 1<<32 - 1 or 1<<64 - 1 ) |
I don't know the answer and I agree with you that these constants should be partof the language or a standard library. Being interested in bit-based algorithmsand data structures, I often need them. This is some code I've been using instead:
// Note the use of << to create an untyped constant. const bitsPerWord = 32 << uint(^uint(0)>>63) // Implementation-specific size of int and uint in bits. const BitsPerWord = bitsPerWord // either 32 or 64 // Implementation-specific integer limit values. const ( MaxInt = 1<<(BitsPerWord-1) - 1 // either 1<<31 - 1 or 1<<63 - 1 MinInt = -MaxInt - 1 // either -1 << 31 or -1 << 63 MaxUint = 1<<BitsPerWord - 1 // either 1<<32 - 1 or 1<<64 - 1 )
Taken from: http://code.google.com/p/go-bit/
I don't see MaxInt, MinInt or MaxUInt defined in there. My question
was referring to the int and uint types, not int8, int16, int32,
int64, etc. which, as I stated in my original post, already have max
and min values defined in the math package. I use int more often than
int32 or int64, for example, and I would still like to test for
overflow.
Best,
Ethan
No problem.
> That seems like an oversight to me but I recall
> it was a religious issue for some--along the lines of, "if you need to
> know the size of an int then you should be using a type whose size you
> control." I don't get that--it seems that an introspective program
> needs to know what is happening no matter how the decisions were
> made--so perhaps others may remember a better reason why this is not
> just an oversight.
Yeah, I am not planning on doing bit manipulation or something with
int. What I had in mind was something like testing whether or not I
could successfully multiply x and y without overflow:
if x < 0 || y < 0 { panic("Negative dimensions") }
if MaxInt / x < y { panic("Overflow") }
size := x * y
Ethan
--
Michael T. Jones | Chief Technology Advocate | m...@google.com | +1
650-335-5765
Is there something wrong with using the values in src/pkg/math/const.go
changeset: 2801:941877ee2f7fuser: Rob Pike <r...@golang.org>date: Tue Jul 28 12:57:33 2009 -0700summary: constants for integer limits
--
size, highProduct := x*y;
if highProduct != 0 { panic("Overflow") }
--