This is what happens:
Code: Select all
program Type_Casting_cont;
var Wrd1, Wrd2: word;
Percentage: byte;
procedure Proc(TotalMemSize: word);
var I: byte;
begin
// ------------ test ------------- //
Wrd1 := 3968; // line X
Percentage := 60; // line Y
Wrd2 := Wrd1 * Percentage div 100; // Wrd2 = correct (2380)
Wrd2 := TotalMemSize * Percentage div 100; // Wrd2 = faulty (414)
// ------------ end test ------------- //
end;
begin
Proc(3968);
end.
There is more: If I move line X or line Y or both to the main function (just before the call to Proc, then both calculations give the value 414!
So, this code
Code: Select all
program Type_Casting_cont;
var Wrd1, Wrd2: word;
Percentage: byte;
procedure Proc(TotalMemSize: word);
var I: byte;
begin
// ------------ test ------------- //
// Wrd1 := 3968;
Percentage := 60;
Wrd2 := Wrd1 * Percentage div 100; // faulty (414)
Wrd2 := TotalMemSize * Percentage div 100; // faulty (414)
// ------------ end test ------------- //
end;
begin
Wrd1 := 3968; // if placed here, both calculations are wrong
Proc(3968);
end.
Wierd eh?
p.s. not so wierd (I had a look to the listing now): in the "correct" case the compiler did not use the variables Wrd1 and Percentage, it simply used the value constants assigned to them (a code optimisation, called "Constant propagation" in the helpfile).
In the other cases it used the variables, and then the calculation method is different and only correct if the left hand side variable can hold the intermediate result (=DWord), or explicite typecasting is used:
Code: Select all
Wrd2 := Dword(TotalMemSize * Percentage div 100);
p.s. Still looks weird afterall. The mP programmer can not know how the compiler will calculate things (how the code will be optimised), and pascal code that looks the same should give the same result. Optimisation should never show side effects to the user! Here the different calculation/typecasting rules for constants and variables are the reason.