# Fixnum::{MIN,MAX}

**URL:** https://rubytalk.org/t/fixnum-min-max/19779
**Category:** ruby-talk
**Created:** [27 July 2005 21:00 UTC](https://rubytalk.org/t/fixnum-min-max/19779 "2005-07-27T21:00:28Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Nikolai\_Weibull](https://avatars.discourse-cdn.com/v4/letter/n/ce7236/32.png) [@Nikolai\_Weibull](https://rubytalk.org/u/Nikolai_Weibull)
#### Post date: [27 July 2005 21:00 UTC](https://rubytalk.org/t/fixnum-min-max/19779/1 "2005-07-27T21:00:28Z")

</div>

This may be silly, considering that Fixnums are converted to Bignums if  
necessary, but is there no room for having MIN and MAX constants for  
Fixnums? This is my C talking, but it’s kind of nice to have these  
kinds of limits for certain algorithms and “default” values. Any  
input?,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai

> **···**
>
> --  
> Nikolai Weibull: now available free of charge at [http://bitwi.se/](http://bitwi.se/)!  
> Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.  
> main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

---

<div class="post-metadata">

### Author: ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)
#### Post date: [27 July 2005 21:06 UTC](https://rubytalk.org/t/fixnum-min-max/19779/2 "2005-07-27T21:06:05Z")

</div>

I too have wished for them in the past.

James Edward Gray II

> **···**
>
> On Jul 27, 2005, at 4:00 PM, Nikolai Weibull wrote:
> 
> > This may be silly, considering that Fixnums are converted to Bignums if  
> > necessary, but is there no room for having MIN and MAX constants for  
> > Fixnums? This is my C talking, but it’s kind of nice to have these  
> > kinds of limits for certain algorithms and “default” values. Any  
> > input?,

---

<div class="post-metadata">

### Author: ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)
#### Post date: [27 July 2005 21:12 UTC](https://rubytalk.org/t/fixnum-min-max/19779/3 "2005-07-27T21:12:28Z")

</div>

awesome! i have this everywhere:

&nbsp;&nbsp;&nbsp;class Fixnum  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N\_BYTES = [42].pack('i').size  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N\_BITS = N\_BYTES \* 8  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAX = 2 \*\* (N\_BITS - 2) - 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MIN = -MAX - 1  
&nbsp;&nbsp;&nbsp;end

cheers.

-a

> **···**
>
> On Thu, 28 Jul 2005, Nikolai Weibull wrote:
> 
> > This may be silly, considering that Fixnums are converted to Bignums if  
> > necessary, but is there no room for having MIN and MAX constants for  
> > Fixnums? This is my C talking, but it’s kind of nice to have these  
> > kinds of limits for certain algorithms and “default” values. Any  
> > input?,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > My religion is very simple. My religion is kindness.  
> > --Tenzin Gyatso
> 
> ===============================================================================

---

<div class="post-metadata">

### Author: ![Berger\_Daniel1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Berger\_Daniel1](https://rubytalk.org/u/Berger_Daniel1)
#### Post date: [27 July 2005 21:24 UTC](https://rubytalk.org/t/fixnum-min-max/19779/4 "2005-07-27T21:24:39Z")

</div>

Ara.T.Howard wrote:

> > This may be silly, considering that Fixnums are converted to Bignums if  
> > necessary, but is there no room for having MIN and MAX constants for  
> > Fixnums? This is my C talking, but it’s kind of nice to have these  
> > kinds of limits for certain algorithms and “default” values. Any  
> > input?,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai
> 
> awesome! i have this everywhere:
> 
> &nbsp;&nbsp;&nbsp;class Fixnum  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N\_BYTES = [42].pack('i').size  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N\_BITS = N\_BYTES \* 8  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAX = 2 \*\* (N\_BITS - 2) - 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MIN = -MAX - 1  
> &nbsp;&nbsp;&nbsp;end
> 
> cheers.
> 
> -a

Signed or unsigned max?

Solaris 10:

/\* maxnum.c \*/  
#include \<stdio.h\>  
#include \<values.h\>

int main(){  
&nbsp;&nbsp;&nbsp;&nbsp;printf("Max int: %i\n", MAXINT);  
&nbsp;&nbsp;&nbsp;&nbsp;printf("Max long: %li\n", MAXLONG);

&nbsp;&nbsp;&nbsp;&nbsp;return 0;  
}

djberge@~/programming/C-568\>gcc -Wall -o maxnum maxnum.c  
djberge@~/programming/C-569\>./maxnum  
Max int: 2147483647  
Max long: 2147483647

djberge@~/programming/C-570\>gcc -Wall -m64 -o maxnum maxnum.c  
djberge@~/programming/C-571\>./maxnum  
Max int: 2147483647  
Max long: 9223372036854775807

Regards,

Dan

> **···**
>
> > On Thu, 28 Jul 2005, Nikolai Weibull wrote:

---

<div class="post-metadata">

### Author: ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)
#### Post date: [27 July 2005 21:36 UTC](https://rubytalk.org/t/fixnum-min-max/19779/5 "2005-07-27T21:36:41Z")

</div>

> Ara.T.Howard wrote:
> 
> > > This may be silly, considering that Fixnums are converted to Bignums if  
> > > necessary, but is there no room for having MIN and MAX constants for  
> > > Fixnums? This is my C talking, but its kind of nice to have these  
> > > kinds of limits for certain algorithms and default values. Any  
> > > input?,  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai
> > 
> > awesome! i have this everywhere:
> > 
> > &nbsp;&nbsp;&nbsp;class Fixnum  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N\_BYTES = [42].pack('i').size  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N\_BITS = N\_BYTES \* 8  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAX = 2 \*\* (N\_BITS - 2) - 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MIN = -MAX - 1  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > cheers.
> > 
> > -a
> 
> Signed or unsigned max?

maybe we are talking at cross purposes: as far as i know there is no such as an  
'unsigned' Fixnum - we only have signed fixnums which loses one bit and the  
VALUE/Fixnum thing which loses another. so i assumed the OP wanted to know the  
largest/smallest value that could be represented by a fixnum:

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;class Fixnum  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N\_BYTES = [42].pack('i').size  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N\_BITS = N\_BYTES \* 8  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAX = 2 \*\* (N\_BITS - 2) - 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MIN = -MAX - 1  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;p(Fixnum::MAX)  
&nbsp;&nbsp;&nbsp;p(Fixnum::MAX.class)  
&nbsp;&nbsp;&nbsp;p((Fixnum::MAX + 1).class)

&nbsp;&nbsp;&nbsp;p(Fixnum::MIN)  
&nbsp;&nbsp;&nbsp;p(Fixnum::MIN.class)  
&nbsp;&nbsp;&nbsp;p((Fixnum::MIN - 1).class)

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;1073741823  
&nbsp;&nbsp;&nbsp;Fixnum  
&nbsp;&nbsp;&nbsp;Bignum  
&nbsp;&nbsp;&nbsp;-1073741824  
&nbsp;&nbsp;&nbsp;Fixnum  
&nbsp;&nbsp;&nbsp;Bignum

make sense?

> Solaris 10:
> 
> /\* maxnum.c \*/  
> #include \<stdio.h\>  
> #include \<values.h\>
> 
> int main(){  
> &nbsp;&nbsp;printf("Max int: %i\n", MAXINT);  
> &nbsp;&nbsp;printf("Max long: %li\n", MAXLONG);
> 
> &nbsp;&nbsp;return 0;  
> }
> 
> djberge@~/programming/C-568\>gcc -Wall -o maxnum maxnum.c  
> djberge@~/programming/C-569\>./maxnum  
> Max int: 2147483647  
> Max long: 2147483647
> 
> djberge@~/programming/C-570\>gcc -Wall -m64 -o maxnum maxnum.c  
> djberge@~/programming/C-571\>./maxnum  
> Max int: 2147483647  
> Max long: 9223372036854775807

sure - but these are ints not fixnums ?

cheers.

-a

> **···**
>
> On Thu, 28 Jul 2005, Daniel Berger wrote:
> 
> > > On Thu, 28 Jul 2005, Nikolai Weibull wrote:
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > My religion is very simple. My religion is kindness.  
> > --Tenzin Gyatso
> 
> ===============================================================================

---

<div class="post-metadata">

### Author: ![Nikolai\_Weibull](https://avatars.discourse-cdn.com/v4/letter/n/ce7236/32.png) [@Nikolai\_Weibull](https://rubytalk.org/u/Nikolai_Weibull)
#### Post date: [27 July 2005 21:57 UTC](https://rubytalk.org/t/fixnum-min-max/19779/6 "2005-07-27T21:57:41Z")

</div>

Daniel Berger wrote:

> Ara.T.Howard wrote:

> \> \> This may be silly, considering that Fixnums are converted to  
> \> \> Bignums if necessary, but is there no room for having MIN and MAX  
> \> \> constants for Fixnums? This is my C talking, but it’s kind of  
> \> \> nice to have these kinds of limits for certain algorithms and  
> \> \> “default” values. Any input?,  
> \> \> nikolai

> \> awesome! i have this everywhere:  
> \>  
> \> class Fixnum  
> \> N\_BYTES = [42].pack('i').size  
> \> N\_BITS = N\_BYTES \* 8  
> \> MAX = 2 \*\* (N\_BITS - 2) - 1  
> \> MIN = -MAX - 1  
> \> end  
> \>  
> \> cheers.

> Signed or unsigned max?

I don’t know if you’re trying to be clever, but as far as I know, it’d  
be the value of LONG\_MAX, as that’s how Fixnums are currently being  
stored. And I don’t see how it matters if you’re on a 32 or 64 bit  
machine (below), as all one’s interested in is the maximum value that  
can be stored in a Fixnum (for sentinel values and so on). Ara’s  
suggestion is a bit flawed, as it assumes a two’s-complement arithmetic  
for the MIN calculation, but who can blame him? 🙂

I was perhaps not so much saying that we \_must\_ have MAX and MIN of  
Fixnums, as a way of having good sentinel values when dealing with  
Fixnums. I was half-expecting a whole thread on duck-typing,  
meta-programming, IoC, or other “possible” solutions to such a problem,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai

> **···**
>
> > \> On Thu, 28 Jul 2005, Nikolai Weibull wrote:
> 
> > djberge@~/programming/C-570\>gcc -Wall -m64 -o maxnum maxnum.c  
> > djberge@~/programming/C-571\>./maxnum  
> > Max int: 2147483647  
> > Max long: 9223372036854775807
> 
> --  
> Nikolai Weibull: now available free of charge at [http://bitwi.se/\](http://bitwi.se/%5C)!  
> Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.  
> main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

---

<div class="post-metadata">

### Author: ![Berger\_Daniel1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Berger\_Daniel1](https://rubytalk.org/u/Berger_Daniel1)
#### Post date: [27 July 2005 21:53 UTC](https://rubytalk.org/t/fixnum-min-max/19779/7 "2005-07-27T21:53:07Z")

</div>

Ara.T.Howard wrote:

\<snip\>

> maybe we are talking at cross purposes: as far as i know there is no such as an  
> 'unsigned' Fixnum - we only have signed fixnums which loses one bit and the  
> VALUE/Fixnum thing which loses another. so i assumed the OP wanted to know the  
> largest/smallest value that could be represented by a fixnum:
> 
> &nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
> &nbsp;&nbsp;&nbsp;class Fixnum  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N\_BYTES = [42].pack('i').size  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;N\_BITS = N\_BYTES \* 8  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MAX = 2 \*\* (N\_BITS - 2) - 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MIN = -MAX - 1  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;p(Fixnum::MAX)  
> &nbsp;&nbsp;&nbsp;p(Fixnum::MAX.class)  
> &nbsp;&nbsp;&nbsp;p((Fixnum::MAX + 1).class)
> 
> &nbsp;&nbsp;&nbsp;p(Fixnum::MIN)  
> &nbsp;&nbsp;&nbsp;p(Fixnum::MIN.class)  
> &nbsp;&nbsp;&nbsp;p((Fixnum::MIN - 1).class)
> 
> &nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
> &nbsp;&nbsp;&nbsp;1073741823  
> &nbsp;&nbsp;&nbsp;Fixnum  
> &nbsp;&nbsp;&nbsp;Bignum  
> &nbsp;&nbsp;&nbsp;-1073741824  
> &nbsp;&nbsp;&nbsp;Fixnum  
> &nbsp;&nbsp;&nbsp;Bignum
> 
> make sense?
> 
> > Solaris 10:
> > 
> > /\* maxnum.c \*/  
> > #include \<stdio.h\>  
> > #include \<values.h\>
> > 
> > int main(){  
> > &nbsp;&nbsp;printf("Max int: %i\n", MAXINT);  
> > &nbsp;&nbsp;printf("Max long: %li\n", MAXLONG);
> > 
> > &nbsp;&nbsp;return 0;  
> > }
> > 
> > djberge@~/programming/C-568\>gcc -Wall -o maxnum maxnum.c  
> > djberge@~/programming/C-569\>./maxnum  
> > Max int: 2147483647  
> > Max long: 2147483647
> > 
> > djberge@~/programming/C-570\>gcc -Wall -m64 -o maxnum maxnum.c  
> > djberge@~/programming/C-571\>./maxnum  
> > Max int: 2147483647  
> > Max long: 9223372036854775807
> 
> sure - but these are ints not fixnums ?

True.

But if we add Fixnum::MIN/MAX, don't we have to add Integer::MIN/MAX and Bignum::MIN/MAX? What about Numeric or Float?

My gut feeling is that this will lead to confusion. Maybe I'm wrong, though.

Dan

> **···**
>
> > On Thu, 28 Jul 2005, Daniel Berger wrote:

---

<div class="post-metadata">

### Author: ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)
#### Post date: [27 July 2005 21:58 UTC](https://rubytalk.org/t/fixnum-min-max/19779/8 "2005-07-27T21:58:47Z")

</div>

you have a point. perhaps

&nbsp;&nbsp;&nbsp;require 'limits'

&nbsp;&nbsp;&nbsp;p Bignum::MAX  
&nbsp;&nbsp;&nbsp;p Integer::MAX

etc. that was new limits could be added later...

btw. is there a Bignum::MAX?

-a

> **···**
>
> On Thu, 28 Jul 2005, Daniel Berger wrote:
> 
> > True.
> > 
> > But if we add Fixnum::MIN/MAX, don't we have to add Integer::MIN/MAX and Bignum::MIN/MAX? What about Numeric or Float?
> > 
> > My gut feeling is that this will lead to confusion. Maybe I'm wrong, though.
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > My religion is very simple. My religion is kindness.  
> > --Tenzin Gyatso
> 
> ===============================================================================

---

<div class="post-metadata">

### Author: ![Berger\_Daniel1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Berger\_Daniel1](https://rubytalk.org/u/Berger_Daniel1)
#### Post date: [27 July 2005 22:03 UTC](https://rubytalk.org/t/fixnum-min-max/19779/9 "2005-07-27T22:03:14Z")

</div>

Ara.T.Howard wrote:

> **···**
>
> > On Thu, 28 Jul 2005, Daniel Berger wrote:
> > 
> > > True.
> > > 
> > > But if we add Fixnum::MIN/MAX, don't we have to add Integer::MIN/MAX and Bignum::MIN/MAX? What about Numeric or Float?
> > > 
> > > My gut feeling is that this will lead to confusion. Maybe I'm wrong, though.
> > 
> > you have a point. perhaps
> > 
> > &nbsp;&nbsp;&nbsp;require 'limits'
> > 
> > &nbsp;&nbsp;&nbsp;p Bignum::MAX  
> > &nbsp;&nbsp;&nbsp;p Integer::MAX
> > 
> > etc. that was new limits could be added later...
> > 
> > btw. is there a Bignum::MAX?
> > 
> > -a
> 
> There is if there's an Integer::MAX. 😃
> 
> Dan

---

<div class="post-metadata">

### Author: ![Nikolai\_Weibull](https://avatars.discourse-cdn.com/v4/letter/n/ce7236/32.png) [@Nikolai\_Weibull](https://rubytalk.org/u/Nikolai_Weibull)
#### Post date: [27 July 2005 22:04 UTC](https://rubytalk.org/t/fixnum-min-max/19779/10 "2005-07-27T22:04:47Z")

</div>

Ara.T.Howard wrote:

> btw. is there a Bignum::MAX?

Sure, though its value would fluctuate with the amount of memory  
currently available,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nikolai

> **···**
>
> --  
> Nikolai Weibull: now available free of charge at [http://bitwi.se/\](http://bitwi.se/%5C)!  
> Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.  
> main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}

---

<div class="post-metadata">

### Author: ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)
#### Post date: [27 July 2005 22:11 UTC](https://rubytalk.org/t/fixnum-min-max/19779/11 "2005-07-27T22:11:27Z")

</div>

it does seems that way - so maybe impossibleto determine/store since saying

class Bignum  
&nbsp;&nbsp;&nbsp;MAX = do\_something\_to\_determine  
end

would exhaust memory...

an estimate as string perhaps?

-a

> **···**
>
> On Thu, 28 Jul 2005, Nikolai Weibull wrote:
> 
> > Ara.T.Howard wrote:
> > 
> > > btw. is there a Bignum::MAX?
> > 
> > Sure, though its value would fluctuate with the amount of memory  
> > currently available,
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > My religion is very simple. My religion is kindness.  
> > --Tenzin Gyatso
> 
> ===============================================================================
