Minus
│
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
In ASCII, the character code decimal 45
(or hexadecimal 2D
) is defined to be
-
(hyphen-minus).
Occurrences
The symbol -
(pronounced “minus”) is used to
- indicate the negative sign of a number literal
- subtract two numbers (using infix notation)
- form the difference of two sets.
program minusDemo(input, output, stderr);
var
x: longint;
g: longint;
m: set of (foo, bar);
begin
// unary operator: negative sign
x := -42; // x becomes negative 42
// binary operator: difference of numbers
g := 144 - 169; // g becomes negative 25
// binary operator: difference of sets
m := [foo, bar] - [bar]; // m becomes {foo}
end.
Minus is also a unary operator.
You can write such stupid expressions as ----+--+--+--8
.
Operation with numbers
Beware:
The result's target of subtractions (and sign inversions) should be a signed integer.
If it's not, with {$rangechecks}
directive enabled, it will possibly cause a run-time error.
In any other case an arithmetically wrong result is produced.
program faultySubtraction(input, output, stderr);
var
x, y: longword;
begin
y := 1;
{$push}
{$rangechecks off} // otherwise the next expression
x := 0 - y; // yields RTE 201
{$pop}
writeLn(x);
end.
This program prints 4294967295
, which equals to high(longword)
because of the (unsigned) integer overflow.
However note, there are situations where negative signs do not necessarily provoke an error. Attempting to store a “negative” zero won't harm:
program minusOperations(input, output, stderr);
{$rangeChecks on}
var
n: longword;
begin
// "negative" zero equals to (sign-less) zero
n := 0;
n := -n;
Also note, arithmetic integer operations are done with the processor's native integer type.
Although intermediate results fall out of range, the code generated by {$rangechecks on}
ensures the type's boundaries aren't exceeded after the result's already stored.
// intermediate results can be out of range:
n := 1;
n := n - n - n + 1;
end.
This whole program will finish with an exitCode
of zero (that means “successful” [actual value might vary among platforms]).
In consequence of the latter mentioned behavior:
Integer overflows in the processor's native type can not be caught (in that way).
In assembler you would have access evaluating several status flags, but in Pascal you do not.
Instead you can use {$overflowChecks}
which will evoke a run-time error.
program overflowChecksDemo(input, output, stderr);
{$overflowChecks on}
var
n: nativeInt;
begin
n := high(n);
n := n + 1; // will yield RTE 215
end.
The generated code by the {$overflowChecks}
directive is the most efficient, since it directly uses available status flag evaluation instructions (e.g. “jump if overflow occured”).
On the downside, a once triggered RTE will inevitably terminate the program.
If this is regarded as counter-productive, you would have to recognize situations leading to overflows on your own:
program overflowAnticipation(input, output, stderr);
var
n: {$ifdef CPU64} int64 {$else} longint {$endif};
begin
// untreated
n := high(n);
n := n + 1;
writeLn(n); // prints low(n)
// anticipated: reverse operation and comparison w/ 1st operand
n := high(n);
if high(n) - 1 < n then
begin
halt(1);
end;
n := n + 1;
writeLn(n);
end.
Obviously this won't be as efficient as testing the overflow status flag, and here, without any re-used constant, is even prone to programming errors (consider the situation changing the subtrahend in one line of code only).
single characters |
|
character pairs |
|