Results 1 to 3 of 3

Thread: Problem with Integer vs LongWord in Delphi and FPC

  1. #1

    Problem with Integer vs LongWord in Delphi and FPC

    System: Windows XP SP2, P4 3GHz
    Compiler/IDE: Dephi 5.0 and FPC 2.1.4
    Libraries/API: None

    I've made a little testing program:


    Code:
    program integer_test;
    
    {$APPTYPE CONSOLE}
    var
      i: integer;
      w: longword;
      x, y: longword;
    begin
      x := $FFFFFFF;  // 268435455
      y := $FFFFFFFB; // 4294967291
      i := abs(x - y);
      writeln(i);     // Delphi output: 268435460, FPC output -4 (!!!)
      w := abs(x - y);
      writeln(w);     // Delphi output: 268435460, FPC output 4026531836
      writeln;
      i := x - y;
      writeln(i);     // Delphi output: 268435460, FPC output 4 (!!!)
      w := x - y;
      writeln(w);     // Delphi output: 268435460, FPC output 4026531836
      writeln;
      readln;
    end.
    I compile this with Delphi and the output is:

    Code:
    268435460
    268435460
    
    268435460
    268435460
    Then I compile this with FreePascal using the following command line:

    fpc integer_test.dpr -CX -XX -Xs -B -Co -al

    The output is:
    Code:

    Code:
    -4
    4026531836
    4
    268435460

    Notice that
    i := abs(x - y);
    writeln(i); -> FPC writes a negative number after abs() function

    Why does this happen?

    Is there an option/command line switch in FPC to get the same result as with Delphi?

    Thanks
    Last edited by Jimmy Valavanis; 30-03-2012 at 05:14 PM.

  2. #2

    Problem with Integer vs LongWord in Delphi and FPC

    The reason is probably that in fpc mode, and integer is -$8000..$7fff. Use a longint or objfpc mode.

  3. #3

    Problem with Integer vs LongWord in Delphi and FPC

    Thanks, I thing that helps.
    Last edited by Jimmy Valavanis; 30-03-2012 at 05:15 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •