[go-nuts] MAX/MIN NUMBER of each integer type

19,557 views
Skip to first unread message

Norio TANAKA

unread,
Apr 16, 2010, 11:25:19 AM4/16/10
to golang-nuts
Hi,

In C, MAX/MIN NUMBER of integer types are provided
in limits.h header file.
How can I get MAX/MIN NUMBER of each integer type?

Thanks in advance,
N.Tanaka


--
Subscription settings: http://groups.google.com/group/golang-nuts/subscribe?hl=en

Ostsol

unread,
Apr 16, 2010, 12:12:20 PM4/16/10
to golang-nuts
In C the sizes of integer types are entirely implementation dependent
and there is no way to determine their sizes in the preprocessor
except through those constants. Go has no built-in preprocessor and
it does have types with explicit sizes that are not implementation
dependent, making such size constants unnecessary. int64, for
example, has a maximum of 2^32 - 1. Float point types presumably
follow IEEE standards.

-Daniel

peterGo

unread,
Apr 16, 2010, 12:22:27 PM4/16/10
to golang-nuts
Norio,

The math package has some of these values.

Peter

peterGo

unread,
Apr 16, 2010, 12:26:54 PM4/16/10
to golang-nuts
Daniel

On Apr 16, 12:12 pm, Ostsol <ost...@gmail.com> wrote:
> int64, for
> example, has a maximum of 2^32 - 1.

MaxInt64 = 1<<63 - 1 = 2^64 - 1

Peter

Norio TANAKA

unread,
Apr 16, 2010, 12:47:31 PM4/16/10
to Ostsol, golang-nuts
Hi, Ostsol-san,

Thank you for your reply.

I thought that for all Go programmers, there might be those constant
declarations somewhere in the package.

I will declare the constants in my package if necessary.

N.Tanaka

Norio TANAKA

unread,
Apr 16, 2010, 12:59:42 PM4/16/10
to peterGo, golang-nuts
Peter-san,

Thanks!!!

I found the constant declarations.
I think I want to use those ones.

N.Tanaka

peterGo

unread,
Apr 16, 2010, 1:24:05 PM4/16/10
to golang-nuts
> MaxInt64 = 1<<63 - 1 = 2^64 - 1

Should have been written as:

MaxInt64 = 1<<63 - 1 = 2^63 - 1

On Apr 16, 12:26 pm, peterGo <go.peter...@gmail.com> wrote:
> Daniel
>
> On Apr 16, 12:12 pm, Ostsol <ost...@gmail.com> wrote:
>
> > int64, for
> > example, has a maximum of 2^32 - 1.
>
> MaxInt64 = 1<<63 - 1 = 2^64 - 1
>
> Peter


peterGo

unread,
Apr 16, 2010, 1:42:00 PM4/16/10
to golang-nuts
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)

Peter

peterGo

unread,
Apr 16, 2010, 1:52:41 PM4/16/10
to golang-nuts
The constants should have been written:

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(^uint(0) >> 1)
const MinInt = -MaxInt - 1


On Apr 16, 1:42 pm, peterGo <go.peter...@gmail.com> wrote:
> 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

Peter Froehlich

unread,
Apr 16, 2010, 2:47:10 PM4/16/10
to peterGo, golang-nuts
Hi all,

Marginally related: I implemented some bitsets recently and I didn't
want to tie myself to int32 or int64 directly. So I used this kludge
to find how many bits an int has:

var bits_per_int int = unsafe.Sizeof(bits_per_int)*8

Does anyone know a more elegant way of doing this? I guess you could
run a for loop over the maximum int value and shift bits out one at a
time until you get 0? Anything slightly more direct?

Cheers,
Peter
--
Peter H. Froehlich <http://www.cs.jhu.edu/~phf/>
Senior Lecturer | Director, Johns Hopkins Gaming Lab

Rob 'Commander' Pike

unread,
Apr 16, 2010, 3:13:37 PM4/16/10
to Peter Froehlich, peterGo, golang-nuts

On Apr 16, 2010, at 11:47 AM, Peter Froehlich wrote:

> Hi all,
>
> Marginally related: I implemented some bitsets recently and I didn't
> want to tie myself to int32 or int64 directly. So I used this kludge
> to find how many bits an int has:
>
> var bits_per_int int = unsafe.Sizeof(bits_per_int)*8
>
> Does anyone know a more elegant way of doing this? I guess you could
> run a for loop over the maximum int value and shift bits out one at a
> time until you get 0? Anything slightly more direct?

I suggest using the shift trick because you don't need package unsafe.

-rob

peterGo

unread,
Apr 16, 2010, 3:45:53 PM4/16/10
to golang-nuts
Peter,

For a constant value:

const IntBits = (1 + ^uint(0)>>32&1) * 32

Peter

On Apr 16, 2:47 pm, Peter Froehlich <peter.hans.froehl...@gmail.com>
wrote:

peterGo

unread,
Apr 16, 2010, 5:47:33 PM4/16/10
to golang-nuts
Peter,

If you want to be certain that an error in the constant for the bit-
width of the implementation-specific int and uint numeric types never
goes undetected, even if the Go language specification changes, you
could use a package init() function to verify the value. For example,

func init() {
// The int and uint numeric types are IntBits wide.
// IntBits is implementation-specific
intbits := 0
for uibits := ^uint(0); uibits != 0; uibits >>= 1 {
intbits++
}
if int(IntBits) != intbits {
panic("const IntBits invalid")
}
}

// The int and uint numeric types are IntBits wide.
const IntBits = (1 + ^uint(0)>>32&1) * 32

Peter

Norio TANAKA

unread,
Apr 17, 2010, 2:24:35 AM4/17/10
to golang-nuts
Hi, all,

Thanks for discussion about this subject.
I think that if it is possible, describing how to get
these values in some FAQ is a great help to all.

Best regards,
N.Tanaka

Ethan Burns

unread,
Mar 15, 2012, 11:22:43 AM3/15/12
to golan...@googlegroups.com
On Friday, April 16, 2010 1:42:00 PM UTC-4, peterGo wrote:
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)

Sorry to bring up an old thread, but I am wondering why these aren't defined in the math package along with MaxInt8 and friends.  It seems that they would be used fairly often, for example, when testing for overflow.  Is there a better way to test this than using the max and min values?


Best,
Ethan 

Stefan Nilsson

unread,
Mar 15, 2012, 12:32:41 PM3/15/12
to golan...@googlegroups.com
Ethan,

I don't know the answer and I agree with you that these constants should be part
of the language or a standard library. Being interested in bit-based algorithms
and 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/

Ethan Burns

unread,
Mar 15, 2012, 1:07:03 PM3/15/12
to golan...@googlegroups.com
On Thursday, March 15, 2012 12:32:41 PM UTC-4, snilsson wrote:
I don't know the answer and I agree with you that these constants should be part
of the language or a standard library. Being interested in bit-based algorithms
and 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/

Thanks.  There is also some code for computing these values posted in a previous message in this thread.  I think that it is easy enough to grab these values from go-bit via go install, however, it somehow seems 'heavy-weight' to install an entire bit-set package just to get MaxInt.  If there is some reason that they aren't included in math then maybe I will make my own package for just these three values.


Best,
Ethan

Ethan Burns

unread,
Mar 15, 2012, 2:57:04 PM3/15/12
to Michael Jones, golan...@googlegroups.com
On Thu, Mar 15, 2012 at 2:49 PM, Michael Jones <m...@google.com> wrote:
>
> Is there something wrong with using the values in src/pkg/math/const.go

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

Ethan Burns

unread,
Mar 15, 2012, 3:07:13 PM3/15/12
to Michael Jones, golan...@googlegroups.com
On Thu, Mar 15, 2012 at 3:04 PM, Michael Jones <m...@google.com> wrote:
> Sorry. Misunderstood.

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 Jones

unread,
Mar 15, 2012, 3:04:05 PM3/15/12
to Ethan Burns, golan...@googlegroups.com
Sorry. Misunderstood. 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.

--
Michael T. Jones | Chief Technology Advocate  | m...@google.com |  +1
650-335-5765

Michael Jones

unread,
Mar 15, 2012, 2:50:26 PM3/15/12
to Ethan Burns, golan...@googlegroups.com
...try

go doc math

On Thu, Mar 15, 2012 at 11:49 AM, Michael Jones <m...@google.com> wrote:
Is there something wrong with using the values in src/pkg/math/const.go

changeset:   2801:941877ee2f7f
user:        Rob Pike <r...@golang.org>
date:        Tue Jul 28 12:57:33 2009 -0700
summary:     constants for integer limits
--
Michael T. Jones | Chief Technology Advocate  | m...@google.com |  +1 650-335-5765

Michael Jones

unread,
Mar 15, 2012, 2:49:13 PM3/15/12
to Ethan Burns, golan...@googlegroups.com
Is there something wrong with using the values in src/pkg/math/const.go

changeset:   2801:941877ee2f7f
user:        Rob Pike <r...@golang.org>
date:        Tue Jul 28 12:57:33 2009 -0700
summary:     constants for integer limits

On Thu, Mar 15, 2012 at 10:07 AM, Ethan Burns <burns...@gmail.com> wrote:

Michael Jones

unread,
Mar 15, 2012, 4:05:31 PM3/15/12
to Ethan Burns, golan...@googlegroups.com
Oh, if only you had two-value results in this special form:

size, highProduct := x*y;
if highProduct != 0 { panic("Overflow") }

--

Reply all
Reply to author
Forward
0 new messages