PDA

View Full Version : Problem with Integer vs LongWord in Delphi and FPC



Jimmy Valavanis
24-05-2007, 08:47 AM
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:




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:




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:



-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

dmantione
24-05-2007, 09:01 AM
The reason is probably that in fpc mode, and integer is -$8000..$7fff. Use a longint or objfpc mode.

Jimmy Valavanis
24-05-2007, 09:44 AM
Thanks, I thing that helps.