Fixnum::{MIN,MAX}

Discussion in 'Ruby' started by Nikolai Weibull, Jul 27, 2005.

  1. 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=E2=80=99s kind of nice to have the=
    se
    kinds of limits for certain algorithms and =E2=80=9Cdefault=E2=80=9D valu=
    es. Any
    input?,
    nikolai

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

  2. On Jul 27, 2005, at 4:00 PM, Nikolai Weibull wrote:

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


    I too have wished for them in the past.

    James Edward Gray II
     
    James Edward Gray II, Jul 27, 2005
    #2
    1. Advertising

  3. Nikolai Weibull

    Ara.T.Howard Guest

    --8323328-1747698051-1122498746=:4734
    Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1747698051-1122498746=:4734"

    This message is in MIME format. The first part should be readable text,
    while the remaining parts are likely unreadable without MIME-aware tools.

    --8323328-1747698051-1122498746=:4734
    Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
    Content-Transfer-Encoding: QUOTED-PRINTABLE

    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=E2=80=99s kind of nice to have the=

    se
    > kinds of limits for certain algorithms and =E2=80=9Cdefault=E2=80=9D valu=

    es. Any
    > input?,
    > nikolai


    awesome! i have this everywhere:

    class Fixnum
    N_BYTES =3D [42].pack('i').size
    N_BITS =3D N_BYTES * 8
    MAX =3D 2 ** (N_BITS - 2) - 1
    MIN =3D -MAX - 1
    end

    cheers.

    -a
    --=20
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D
    | 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
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D

    --8323328-1747698051-1122498746=:4734--
    --8323328-1747698051-1122498746=:4734--
     
    Ara.T.Howard, Jul 27, 2005
    #3
  4. Ara.T.Howard wrote:
    > On Thu, 28 Jul 2005, Nikolai Weibull wrote:
    >=20
    >> This may be silly, considering that Fixnums are converted to Bignums i=

    f
    >> necessary, but is there no room for having MIN and MAX constants for
    >> Fixnums? This is my C talking, but it=92s kind of nice to have these
    >> kinds of limits for certain algorithms and =93default=94 values. Any
    >> input?,
    >> nikolai

    >=20
    >=20
    > awesome! i have this everywhere:
    >=20
    > class Fixnum
    > N_BYTES =3D [42].pack('i').size
    > N_BITS =3D N_BYTES * 8
    > MAX =3D 2 ** (N_BITS - 2) - 1
    > MIN =3D -MAX - 1
    > end
    >=20
    > cheers.
    >=20
    > -a


    Signed or unsigned max?

    Solaris 10:

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

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

    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
     
    Daniel Berger, Jul 27, 2005
    #4
  5. Nikolai Weibull

    Ara.T.Howard Guest

    --8323328-1742129316-1122500183=:4734
    Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1742129316-1122500183=:4734"

    This message is in MIME format. The first part should be readable text,
    while the remaining parts are likely unreadable without MIME-aware tools.

    --8323328-1742129316-1122500183=:4734
    Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
    Content-Transfer-Encoding: QUOTED-PRINTABLE

    On Thu, 28 Jul 2005, Daniel Berger wrote:

    > Ara.T.Howard wrote:
    >> On Thu, 28 Jul 2005, Nikolai Weibull wrote:
    >>=20
    >>> 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=92s kind of nice to have these
    >>> kinds of limits for certain algorithms and =93default=94 values. Any
    >>> input?,
    >>> nikolai

    >>=20
    >>=20
    >> awesome! i have this everywhere:
    >>=20
    >> class Fixnum
    >> N_BYTES =3D [42].pack('i').size
    >> N_BITS =3D N_BYTES * 8
    >> MAX =3D 2 ** (N_BITS - 2) - 1
    >> MIN =3D -MAX - 1
    >> end
    >>=20
    >> cheers.
    >>=20
    >> -a

    >
    > Signed or unsigned max?


    maybe we are talking at cross purposes: as far as i know there is no such a=
    s 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:

    harp:~ > cat a.rb
    class Fixnum
    N_BYTES =3D [42].pack('i').size
    N_BITS =3D N_BYTES * 8
    MAX =3D 2 ** (N_BITS - 2) - 1
    MIN =3D -MAX - 1
    end

    p(Fixnum::MAX)
    p(Fixnum::MAX.class)
    p((Fixnum::MAX + 1).class)

    p(Fixnum::MIN)
    p(Fixnum::MIN.class)
    p((Fixnum::MIN - 1).class)

    harp:~ > ruby a.rb
    1073741823
    Fixnum
    Bignum
    -1073741824
    Fixnum
    Bignum

    make sense?

    > Solaris 10:
    >
    > /* maxnum.c */
    > #include <stdio.h>
    > #include <values.h>
    >
    > int main(){
    > printf("Max int: %i\n", MAXINT);
    > printf("Max long: %li\n", MAXLONG);
    >
    > 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
    --=20
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D
    | 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
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
    =3D=3D=3D=3D

    --8323328-1742129316-1122500183=:4734--
    --8323328-1742129316-1122500183=:4734--
     
    Ara.T.Howard, Jul 27, 2005
    #5
  6. Ara.T.Howard wrote:
    > On Thu, 28 Jul 2005, Daniel Berger 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:
    >
    > harp:~ > cat a.rb
    > class Fixnum
    > N_BYTES = [42].pack('i').size
    > N_BITS = N_BYTES * 8
    > MAX = 2 ** (N_BITS - 2) - 1
    > MIN = -MAX - 1
    > end
    >
    > p(Fixnum::MAX)
    > p(Fixnum::MAX.class)
    > p((Fixnum::MAX + 1).class)
    >
    > p(Fixnum::MIN)
    > p(Fixnum::MIN.class)
    > p((Fixnum::MIN - 1).class)
    >
    > harp:~ > ruby a.rb
    > 1073741823
    > Fixnum
    > Bignum
    > -1073741824
    > Fixnum
    > Bignum
    >
    > make sense?
    >
    >> Solaris 10:
    >>
    >> /* maxnum.c */
    >> #include <stdio.h>
    >> #include <values.h>
    >>
    >> int main(){
    >> printf("Max int: %i\n", MAXINT);
    >> printf("Max long: %li\n", MAXLONG);
    >>
    >> 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
     
    Daniel Berger, Jul 27, 2005
    #6
  7. Daniel Berger wrote:

    > Ara.T.Howard wrote:


    > > 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=E2=80=99s kind=

    of
    > > > nice to have these kinds of limits for certain algorithms and
    > > > =E2=80=9Cdefault=E2=80=9D values. Any input?,
    > > > nikolai


    > > awesome! i have this everywhere:
    > >
    > > class Fixnum
    > > N_BYTES =3D [42].pack('i').size
    > > N_BITS =3D N_BYTES * 8
    > > MAX =3D 2 ** (N_BITS - 2) - 1
    > > MIN =3D -MAX - 1
    > > end
    > >
    > > cheers.


    > Signed or unsigned max?


    I don=E2=80=99t know if you=E2=80=99re trying to be clever, but as far as=
    I know, it=E2=80=99d
    be the value of LONG_MAX, as that=E2=80=99s how Fixnums are currently bei=
    ng
    stored. And I don=E2=80=99t see how it matters if you=E2=80=99re on a 32=
    or 64 bit
    machine (below), as all one=E2=80=99s interested in is the maximum value =
    that
    can be stored in a Fixnum (for sentinel values and so on). Ara=E2=80=99s
    suggestion is a bit flawed, as it assumes a two=E2=80=99s-complement arit=
    hmetic
    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 =E2=80=9Cpossible=E2=80=9D solutions to s=
    uch a problem,
    nikolai

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


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

    Ara.T.Howard Guest

    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

    require 'limits'

    p Bignum::MAX
    p Integer::MAX

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

    btw. is there a Bignum::MAX?

    -a
    --
    ===============================================================================
    | 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
    ===============================================================================
     
    Ara.T.Howard, Jul 27, 2005
    #8
  9. 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
    >
    > require 'limits'
    >
    > p Bignum::MAX
    > 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. :-D

    Dan
     
    Daniel Berger, Jul 27, 2005
    #9
  10. Ara.T.Howard wrote:

    > btw. is there a Bignum::MAX?


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

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

    Ara.T.Howard Guest

    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,


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

    class Bignum
    MAX = do_something_to_determine
    end

    would exhaust memory...

    an estimate as string perhaps?

    -a
    --
    ===============================================================================
    | 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
    ===============================================================================
     
    Ara.T.Howard, Jul 27, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lois
    Replies:
    1
    Views:
    3,313
    Ryan Stewart
    Dec 27, 2004
  2. juergen
    Replies:
    3
    Views:
    575
    opalinski from opalpaweb
    Sep 20, 2006
  3. Albert Hopkins

    When is min(a, b) != min(b, a)?

    Albert Hopkins, Jan 21, 2008, in forum: Python
    Replies:
    31
    Views:
    844
    Albert van der Horst
    Feb 4, 2008
  4. Heesob Park

    Why Fixnum===Fixnum is false?

    Heesob Park, May 13, 2009, in forum: Ruby
    Replies:
    5
    Views:
    128
    Joel VanderWerf
    May 14, 2009
  5. MaggotChild
    Replies:
    6
    Views:
    146
    MaggotChild
    Dec 2, 2009
Loading...

Share This Page