Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2000-12

mixed cardinal/signed operations

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jonas Maebe

Hello,

As of tomorrow, the fixes branch snapshot (and as such also the soon-to-be-released 1.0.4) will evaluate divisions which contain a cardinal (unsigned 32bit integer) and a signed expression by first converting both arguments to int64 and then performing the operation with 64bits of precision. The result of such an operation will be an int64 as well.

The reason is that if you have

var
  l: longint;
  d: cardinal;
begin
  l := -1;
  d := 1;
  writeln(l div d);
end.

the program will currently terminate with a division by zero error (the actual error is a division overflow, but the errorcode is the same).

This 64bit evaluation will also be used for addition, substraction and multiplication (if the above operand conditions are fulfilled) when range and/or overflow checking is used. A warning will be issued in all cases.

In general, you won't notice this (except that this 64bit operation is obviously a lot slower than a normal 32bit one), except if you typecast the result back to a pointer, because an int64 can't be converted to a pointer. In that case, either add another typecast to dword/longint in between, or (preferably) typecase all operands of the oepration to either longint or dword so no 64bit evaluation is necessary.

BTW: if someone thinks this 64bit evaluation should be the default for all mathematical operations regardless of whether range and/or overflow checking is enabled, please speak up, because we aren't 100% sure ourselves of how this should be handled.

Jonas

Reply by email