Large Numbers?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew

    #1

    Large Numbers?

    What is the largest number (integer?)
    that I can work with in PHP?

    For example, I have this number:

    1082655076035

    This is actually a date that I need to
    compare with another date.

    I was just going to see if one was greater
    than the other but it doesn't seem to be
    working.

    I can try to convert it to a date object if
    that is the only way to do it...

    Thanks,

    Andrew

  • Michael Austin

    #2
    Re: Large Numbers?

    Andrew wrote:[color=blue]
    > What is the largest number (integer?)
    > that I can work with in PHP?
    >
    > For example, I have this number:
    >
    > 1082655076035
    >
    > This is actually a date that I need to
    > compare with another date.
    >
    > I was just going to see if one was greater
    > than the other but it doesn't seem to be
    > working.
    >
    > I can try to convert it to a date object if
    > that is the only way to do it...
    >
    > Thanks,
    >
    > Andrew
    >[/color]

    By definition on almost all platforms INTEGER is 32 bits (4 bytes) or a
    maximum of 2Billion your number is > 1Trillion.

    from:



    "The size of an integer is platform-dependent, although a maximum value
    of about two billion is the usual value (that's 32 bits signed). PHP
    does not support unsigned integers."

    you want to use float



    "The size of a float is platform-dependent, although a maximum of
    ~1.8e308 with a precision of roughly 14 decimal digits is a common value
    (that's 64 bit IEEE format)."

    Michael Austin.

    Comment

    • Michael Austin

      #3
      Re: Large Numbers?

      Andrew wrote:
      [color=blue]
      > What is the largest number (integer?)
      > that I can work with in PHP?
      >
      > For example, I have this number:
      >
      > 1082655076035
      >
      > This is actually a date that I need to
      > compare with another date.
      >
      > I was just going to see if one was greater
      > than the other but it doesn't seem to be
      > working.
      >
      > I can try to convert it to a date object if
      > that is the only way to do it...
      >
      > Thanks,
      >
      > Andrew
      >[/color]

      .... forgot to mention that most systems now use 64 bits for date/time...

      and if you are thinking I was wrong that 2^32 is 2147483648, integer is
      signed - in otherwords it is 2^31 and the 32nd bit is for +/-.

      2^32 is 4294967296

      2^64 is 184467440737095 51616

      Big difference...

      Michael.

      Comment

      • Tim Van Wassenhove

        #4
        Re: Large Numbers?

        In article <UUNBc.2291$OZ2 .1901@newssvr23 .news.prodigy.c om>, Michael Austin wrote:[color=blue]
        > and if you are thinking I was wrong that 2^32 is 2147483648, integer is
        > signed - in otherwords it is 2^31 and the 32nd bit is for +/-.[/color]

        I think you are mistaken here.
        Complement notation, as in the first (2^32)/2 represent a positive integer
        and the last (2^32)/2 represent a negative integer.

        --
        Tim Van Wassenhove <http://home.mysth.be/~timvw>

        Comment

        • Michael Austin

          #5
          Re: Large Numbers?

          Tim Van Wassenhove wrote:[color=blue]
          > In article <UUNBc.2291$OZ2 .1901@newssvr23 .news.prodigy.c om>, Michael Austin wrote:
          >[color=green]
          >>and if you are thinking I was wrong that 2^32 is 2147483648, integer is
          >>signed - in otherwords it is 2^31 and the 32nd bit is for +/-.[/color]
          >
          >
          > I think you are mistaken here.
          > Complement notation, as in the first (2^32)/2 represent a positive integer
          > and the last (2^32)/2 represent a negative integer.
          >[/color]

          That is correct! If bit 32 is 0 it is positive, if bit 32 is 1 it is
          negative... I think that if you were to use your calculator, you just
          said the same thing I did.. bit 32 (MSB) is for +/-. The maximum value
          in an integer is +/- 2147483648 If you use your calculater 2^32 is
          2147483648*2. So, no, I was not wrong... :) :)

          Michael Austin.
          -- using 32 bit systems before Intel/AMD ever did...



          Comment

          • Tim Van Wassenhove

            #6
            Re: Large Numbers?

            In article <1eWBc.7927$mB1 .6332@newssvr24 .news.prodigy.c om>, Michael Austin wrote:[color=blue]
            > Tim Van Wassenhove wrote:[color=green]
            >> In article <UUNBc.2291$OZ2 .1901@newssvr23 .news.prodigy.c om>, Michael Austin wrote:
            >>[color=darkred]
            >>>and if you are thinking I was wrong that 2^32 is 2147483648, integer is
            >>>signed - in otherwords it is 2^31 and the 32nd bit is for +/-.[/color]
            >>
            >>
            >> I think you are mistaken here.
            >> Complement notation, as in the first (2^32)/2 represent a positive integer
            >> and the last (2^32)/2 represent a negative integer.
            >>[/color]
            >
            > That is correct! If bit 32 is 0 it is positive, if bit 32 is 1 it is
            > negative... I think that if you were to use your calculator, you just
            > said the same thing I did.. bit 32 (MSB) is for +/-. The maximum value
            > in an integer is +/- 2147483648 If you use your calculater 2^32 is
            > 2147483648*2. So, no, I was not wrong... :) :)[/color]

            So this means you have a +0 and a -0 ? ;)

            Think about it, first (2^32)/2 thus from 0 to 2^31-1 represent a
            positive integers. And from from 2^31 to 2^32-1 represent negative
            integers.

            --
            Tim Van Wassenhove <http://home.mysth.be/~timvw>

            Comment

            • Michael Austin

              #7
              Re: Large Numbers?

              Tim Van Wassenhove wrote:[color=blue]
              > In article <1eWBc.7927$mB1 .6332@newssvr24 .news.prodigy.c om>, Michael Austin wrote:
              >[color=green]
              >>Tim Van Wassenhove wrote:
              >>[color=darkred]
              >>>In article <UUNBc.2291$OZ2 .1901@newssvr23 .news.prodigy.c om>, Michael Austin wrote:
              >>>
              >>>
              >>>>and if you are thinking I was wrong that 2^32 is 2147483648, integer is
              >>>>signed - in otherwords it is 2^31 and the 32nd bit is for +/-.
              >>>
              >>>
              >>>I think you are mistaken here.
              >>>Complement notation, as in the first (2^32)/2 represent a positive integer
              >>>and the last (2^32)/2 represent a negative integer.
              >>>[/color]
              >>
              >>That is correct! If bit 32 is 0 it is positive, if bit 32 is 1 it is
              >>negative... I think that if you were to use your calculator, you just
              >>said the same thing I did.. bit 32 (MSB) is for +/-. The maximum value
              >>in an integer is +/- 2147483648 If you use your calculater 2^32 is
              >>2147483648* 2. So, no, I was not wrong... :) :)[/color]
              >
              >
              > So this means you have a +0 and a -0 ? ;)
              >
              > Think about it, first (2^32)/2 thus from 0 to 2^31-1 represent a
              > positive integers. And from from 2^31 to 2^32-1 represent negative
              > integers.
              >[/color]


              I think it is you that must think about it :)
              |< ---- 32 bits ---- >|
              000000000000000 000000000000000 00 = 0
              011111111111111 111111111111111 11 = +2147483647
              100000000000000 000000000000000 00 = -2147483648 not -0.
              111111111111111 111111111111111 11 = -1

              and yes, you are missing one number because the last bit is used for the
              sign...

              to illustrate: try this:

              <?php
              $foo = 2147483647;
              $bar = $foo + 1;
              print "$foo\n";
              print "$foo + 1 = $bar\n";
              print "Now we use settype to type integer and repeat\n";
              settype ($foo,"integer" );
              settype ($bar,"integer" );
              print "$foo\n";
              print "$foo + 1 = $bar\n";
              ?>



              <snide remarks concerning the quality of education these days removed> :) ;)

              Michael Austin.

              Comment

              • Michael Austin

                #8
                Re: Large Numbers?

                Tim Van Wassenhove wrote:
                [color=blue]
                > In article <1eWBc.7927$mB1 .6332@newssvr24 .news.prodigy.c om>, Michael Austin wrote:
                >[color=green]
                >>Tim Van Wassenhove wrote:
                >>[color=darkred]
                >>>In article <UUNBc.2291$OZ2 .1901@newssvr23 .news.prodigy.c om>, Michael Austin wrote:
                >>>
                >>>
                >>>>and if you are thinking I was wrong that 2^32 is 2147483648, integer is
                >>>>signed - in otherwords it is 2^31 and the 32nd bit is for +/-.
                >>>
                >>>
                >>>I think you are mistaken here.
                >>>Complement notation, as in the first (2^32)/2 represent a positive integer
                >>>and the last (2^32)/2 represent a negative integer.
                >>>[/color]
                >>
                >>That is correct! If bit 32 is 0 it is positive, if bit 32 is 1 it is
                >>negative... I think that if you were to use your calculator, you just
                >>said the same thing I did.. bit 32 (MSB) is for +/-. The maximum value
                >>in an integer is +/- 2147483648 If you use your calculater 2^32 is
                >>2147483648* 2. So, no, I was not wrong... :) :)[/color]
                >
                >
                > So this means you have a +0 and a -0 ? ;)
                >
                > Think about it, first (2^32)/2 thus from 0 to 2^31-1 represent a
                > positive integers. And from from 2^31 to 2^32-1 represent negative
                > integers.
                >[/color]

                I do recall a case when supporting a particular FORTRAN complier that
                there was a bug causing it to actually have a result of -0. :) It was
                fixed in a patch...

                Michael.

                Comment

                • Tim Van Wassenhove

                  #9
                  Re: Large Numbers?

                  In article <tCZBc.7992$om2 .6499@newssvr24 .news.prodigy.c om>, Michael Austin wrote:[color=blue]
                  > Tim Van Wassenhove wrote:[color=green]
                  >> In article <1eWBc.7927$mB1 .6332@newssvr24 .news.prodigy.c om>, Michael Austin wrote:
                  >>[color=darkred]
                  >>>Tim Van Wassenhove wrote:
                  >>>
                  >>>>In article <UUNBc.2291$OZ2 .1901@newssvr23 .news.prodigy.c om>, Michael Austin wrote:
                  >>>>
                  >>>>
                  >>>>>and if you are thinking I was wrong that 2^32 is 2147483648, integer is
                  >>>>>signed - in otherwords it is 2^31 and the 32nd bit is for +/-.
                  >>>>
                  >>>>
                  >>>>I think you are mistaken here.
                  >>>>Complemen t notation, as in the first (2^32)/2 represent a positive integer
                  >>>>and the last (2^32)/2 represent a negative integer.
                  >>>>
                  >>>
                  >>>That is correct! If bit 32 is 0 it is positive, if bit 32 is 1 it is
                  >>>negative.. . I think that if you were to use your calculator, you just
                  >>>said the same thing I did.. bit 32 (MSB) is for +/-. The maximum value
                  >>>in an integer is +/- 2147483648 If you use your calculater 2^32 is
                  >>>2147483648*2 . So, no, I was not wrong... :) :)[/color]
                  >>
                  >>
                  >> So this means you have a +0 and a -0 ? ;)
                  >>
                  >> Think about it, first (2^32)/2 thus from 0 to 2^31-1 represent a
                  >> positive integers. And from from 2^31 to 2^32-1 represent negative
                  >> integers.
                  >>[/color]
                  >
                  >
                  > I think it is you that must think about it :)
                  >|< ---- 32 bits ---- >|
                  > 000000000000000 000000000000000 00 = 0
                  > 011111111111111 111111111111111 11 = +2147483647
                  > 100000000000000 000000000000000 00 = -2147483648 not -0.
                  > 111111111111111 111111111111111 11 = -1[/color]

                  I agree with that.

                  When you said that the first bit is used for the sign i thought that you meant
                  0 and 31 bits = +a
                  1 and 31 bits = -a

                  and with that i could not agree:)


                  --
                  Tim Van Wassenhove <http://home.mysth.be/~timvw>

                  Comment

                  • Michael Austin

                    #10
                    Re: Large Numbers?

                    Tim Van Wassenhove wrote:
                    [color=blue]
                    > In article <tCZBc.7992$om2 .6499@newssvr24 .news.prodigy.c om>, Michael Austin wrote:
                    >[color=green]
                    >>Tim Van Wassenhove wrote:
                    >>[color=darkred]
                    >>>In article <1eWBc.7927$mB1 .6332@newssvr24 .news.prodigy.c om>, Michael Austin wrote:
                    >>>
                    >>>
                    >>>>Tim Van Wassenhove wrote:
                    >>>>
                    >>>>
                    >>>>>In article <UUNBc.2291$OZ2 .1901@newssvr23 .news.prodigy.c om>, Michael Austin wrote:
                    >>>>>
                    >>>>>
                    >>>>>
                    >>>>>>and if you are thinking I was wrong that 2^32 is 2147483648, integer is
                    >>>>>>signed - in otherwords it is 2^31 and the 32nd bit is for +/-.
                    >>>>>
                    >>>>>
                    >>>>>I think you are mistaken here.
                    >>>>>Compleme nt notation, as in the first (2^32)/2 represent a positive integer
                    >>>>>and the last (2^32)/2 represent a negative integer.
                    >>>>>
                    >>>>
                    >>>>That is correct! If bit 32 is 0 it is positive, if bit 32 is 1 it is
                    >>>>negative. .. I think that if you were to use your calculator, you just
                    >>>>said the same thing I did.. bit 32 (MSB) is for +/-. The maximum value
                    >>>>in an integer is +/- 2147483648 If you use your calculater 2^32 is
                    >>>>2147483648* 2. So, no, I was not wrong... :) :)
                    >>>
                    >>>
                    >>>So this means you have a +0 and a -0 ? ;)
                    >>>
                    >>>Think about it, first (2^32)/2 thus from 0 to 2^31-1 represent a
                    >>>positive integers. And from from 2^31 to 2^32-1 represent negative
                    >>>integers.
                    >>>[/color]
                    >>
                    >>
                    >>I think it is you that must think about it :)
                    >>|< ---- 32 bits ---- >|
                    >> 000000000000000 000000000000000 00 = 0
                    >> 011111111111111 111111111111111 11 = +2147483647
                    >> 100000000000000 000000000000000 00 = -2147483648 not -0.
                    >> 111111111111111 111111111111111 11 = -1[/color]
                    >
                    >
                    > I agree with that.
                    >
                    > When you said that the first bit is used for the sign i thought that you meant
                    > 0 and 31 bits = +a
                    > 1 and 31 bits = -a
                    >
                    > and with that i could not agree:)
                    >
                    >[/color]

                    We were saying the same thing... just the perspective was skewed...
                    and that all depends on whether or not your CPU is little endian or big
                    endian or as in the case of the Alpha CPU (DEC/HP) it is bi-endian :)

                    Michael Austin.

                    Comment

                    • Chung Leong

                      #11
                      Re: Large Numbers?

                      "Andrew" <andrew@nowhere nohow.com> wrote in message
                      news:JhLBc.1353 7$Wr.5564@newsr ead1.news.pas.e arthlink.net...[color=blue]
                      > What is the largest number (integer?)
                      > that I can work with in PHP?
                      >
                      > For example, I have this number:
                      >
                      > 1082655076035
                      >
                      > This is actually a date that I need to
                      > compare with another date.
                      >
                      > I was just going to see if one was greater
                      > than the other but it doesn't seem to be
                      > working.
                      >
                      > I can try to convert it to a date object if
                      > that is the only way to do it...[/color]

                      Well, you can use floats for numbers beyond an int's range. Since you're
                      just doing comparison, the loss of precision doesn't matter.

                      Getting back to the question, there is practically no limit to how large a
                      integer PHP can handle, as there's the GMP module.


                      Comment

                      • Jochen Daum

                        #12
                        Re: Large Numbers?

                        Hi Chung,

                        On Tue, 22 Jun 2004 18:38:12 -0400, "Chung Leong"
                        <chernyshevsky@ hotmail.com> wrote:
                        [color=blue]
                        >"Andrew" <andrew@nowhere nohow.com> wrote in message
                        >news:JhLBc.135 37$Wr.5564@news read1.news.pas. earthlink.net.. .[color=green]
                        >> What is the largest number (integer?)
                        >> that I can work with in PHP?
                        >>
                        >> For example, I have this number:
                        >>
                        >> 1082655076035
                        >>
                        >> This is actually a date that I need to
                        >> compare with another date.
                        >>
                        >> I was just going to see if one was greater
                        >> than the other but it doesn't seem to be
                        >> working.
                        >>
                        >> I can try to convert it to a date object if
                        >> that is the only way to do it...[/color]
                        >
                        >Well, you can use floats for numbers beyond an int's range. Since you're
                        >just doing comparison, the loss of precision doesn't matter.[/color]

                        Well, if you compare a calculated float to a literal float you should
                        keep in mind that they might not be equal. You should always check if
                        the float is within a certain range of a literal float, due to the
                        limited representation of a float.

                        Use (1.41 - sqrt(2)) < 0.01

                        instead of 1.41 == sqrt(2)

                        This problem is *not* dependent on the precision of a float.

                        HTH, Jochen
                        --
                        Jochen Daum - Cabletalk Group Ltd.
                        PHP DB Edit Toolkit -- PHP scripts for building
                        database editing interfaces.
                        http://sourceforge.net/projects/phpdbedittk/

                        Comment

                        Working...