Differentr Values for intval(float)

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

    Differentr Values for intval(float)

    Consider the following code:

    $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.

    This is encryption code that does a bunch of calculations on signed
    32-bit numbers and ignores any overflows.

    Any ideas how I can do this?

    --Bruce
  • Jerry Stuckle

    #2
    Re: Differentr Values for intval(float)

    Bruce wrote:[color=blue]
    > Consider the following code:
    >
    > $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.
    >
    > This is encryption code that does a bunch of calculations on signed
    > 32-bit numbers and ignores any overflows.
    >
    > Any ideas how I can do this?
    >
    > --Bruce[/color]

    It's never a good idea to depend on hardware dependent features like the
    handling of overflow. They have a tendency to break when you move to a
    different hardware platform.

    I guess you could do some testing and bit manipulation on it, but that's
    adding more complexity to the code (and more chances for it to break).

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Jasen Betts

      #3
      Re: Differentr Values for intval(float)

      On 2006-02-18, Bruce <bvanderweb@gma il.com> wrote:[color=blue]
      > Consider the following code:
      >
      > $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.
      >
      > This is encryption code that does a bunch of calculations on signed
      > 32-bit numbers and ignores any overflows.
      >
      > Any ideas how I can do this?[/color]

      after that do this.

      $AA= (( $AA + 2147483648 ) & 4294967295) - 2147483648 ;

      it should give the result you want on 64-bit machines
      and be harmless on 32-bit machines.

      Bye.
      Jasen

      Comment

      Working...