[fpc-devel] Math.DivMod results should be signed
Michalis Kamburelis
mkambi at poczta.onet.pl
Tue Mar 21 05:37:27 CET 2006
Hi
I'm concerned with using DivMod in cases when Dividend is < 0. DivMod
declaration is
procedure DivMod(Dividend: Integer; Divisor: Word; var Result,
Remainder: Word);
which means that it doesn't allow for Result and Remainder to be < 0.
But when Dividend is < 0, Result and/or Remainder sometimes have to be <
0. For example,
var
UnsignedDivResult, UnsignedModResult: Word;
DivResult: SmallInt absolute UnsignedDivResult;
ModResult: SmallInt absolute UnsignedModResult;
begin
DivMod(-39, 20, UnsignedDivResult, UnsignedModResult);
end;
gets me UnsignedDivResult = 65535 and UnsignedModResult = 65517. Of
course when treating memory as signed values (DivResult and ModResult),
I can "decipher" correct values (-1 and -19) but that's obviously
unclean. Sure I can also just use "div" and "mod" operators, they always
get me correct values, but I understand that doing div and mod
separately may be slower than DivMod on some processors.
I know that DivMod arguments have to stay as Word for Delphi compat. But
in the light of the above, it seems like a good idea to add overloaded
DivMod version that returns Result and Remainder as signed values ?
Michalis
More information about the fpc-devel
mailing list