[fpc-pascal] subtract longwords with rollover

Tomas Hajny XHajT03 at hajny.biz
Thu Nov 28 18:32:16 CET 2013

On Thu, November 28, 2013 18:12, Klaus Hartnegg wrote:
> Sorry, I forgot to mention there is {$Q-} in the source.

I believe that $Q- covers overflow checks, not range checks.

> The trick was indeed to use dec(longint(a),b). However I had missed one
> place in the code, so I got another runtime error from there.
> But why does it only work with longint, and not with longword?
> This looks like a missing declaration of one variant of the dec function.

Not really: it gives the same result if you use any other unsigned type
too (byte, word) and the documentation for Dec() already tells you this
explicitly ("A range check can occur, or an underflow error, if an attempt
it made to decrease X below its minimum value."). (copied from the
documentation sources including the typo - "it made" instead of "is made"
;-) )

It simply does as designed - if someone tries to fit a negative value into
an unsigned variable, range checking detects it. You can avoid it by
either performing an explicit typecast so that the result matches the
variable type (so that the range is not crossed), or by disabling the
range checking (locally for the respective part of code or globally).


More information about the fpc-pascal mailing list