Subtracting Large Numbers

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

    Subtracting Large Numbers

    I am puzzled by the following code:

    <?php
    print "<p>MaxInt=".PH P_INT_MAX;
    $AA = -190668411;
    $BB = -2181087916;
    print "<br>AA=$AA ";
    print "<br>BB=$BB ";
    $AA = (int)$AA + (int)$BB;
    print "<br>AA+BB=$AA" ;
    ?>

    On some systems, I get:

    MaxInt=21474836 47
    AA=-190668411
    BB=-2181087916
    AA+BB=-2338152059

    On others, I get:

    MaxInt=21474836 47
    AA=-190668411
    BB=-2181087916
    AA+BB=192321096 9

    Why the difference?

    Thanks...Bruce
  • Michael Austin

    #2
    Re: Subtracting Large Numbers

    Bruce wrote:[color=blue]
    > I am puzzled by the following code:
    >
    > <?php
    > print "<p>MaxInt=".PH P_INT_MAX;
    > $AA = -190668411;
    > $BB = -2181087916;
    > print "<br>AA=$AA ";
    > print "<br>BB=$BB ";
    > $AA = (int)$AA + (int)$BB;
    > print "<br>AA+BB=$AA" ;
    > ?>
    >
    > On some systems, I get:
    >
    > MaxInt=21474836 47
    > AA=-190668411
    > BB=-2181087916
    > AA+BB=-2338152059
    >
    > On others, I get:
    >
    > MaxInt=21474836 47
    > AA=-190668411
    > BB=-2181087916
    > AA+BB=192321096 9
    >
    > Why the difference?
    >
    > Thanks...Bruce[/color]


    Get the integer value of a variable


    "The maximum value depends on the system. 32 bit systems have a maximum signed
    integer range of -2147483648 to 2147483647. So for example on such a system,
    intval('1000000 000000') will return 2147483647. The maximum signed integer value
    for 64 bit systems is 922337203685477 5807."

    Depends on system, OS, math libraries, cpu type etc...

    --
    Michael Austin.
    Consultant - Available.
    Donations welcomed. Http://www.firstdbasource.com/donations.html
    :)

    Comment

    • Bruce

      #3
      Re: Subtracting Large Numbers

      Actually, this appears to be an issue with how floats are converted to
      ints. For example:

      $BB = -2181087916;
      $AA = (int)$BB;
      $AA = intval($BB);

      On some systems, $AA will be int(-2147483648), which is actually
      consistent with the documentation.

      On most systems, however, $AA will be int(2113879380) , which is the
      same value truncated at 32 bits.

      I actually need the latter behavior, as it needs to duplicate Delphi
      code, which, like C, behaves the same way.

      Any ideas how I can do this?


      On Fri, 17 Feb 2006 23:43:49 GMT, Michael Austin
      <maustin@firstd basource.com> wrote:
      [color=blue]
      >
      >"The maximum value depends on the system. 32 bit systems have a maximum signed
      >integer range of -2147483648 to 2147483647. So for example on such a system,
      >intval('100000 0000000') will return 2147483647. The maximum signed integer value
      >for 64 bit systems is 922337203685477 5807."
      >
      >Depends on system, OS, math libraries, cpu type etc...[/color]

      Comment

      Working...