Difference between revisions of "Minus"
(add content, remove Template:Stub) |
|||
Line 1: | Line 1: | ||
− | |||
{{Minus}} | {{Minus}} | ||
<div style="float:left; margin: 0 10px 10px 0; padding:40px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">-</div> | <div style="float:left; margin: 0 10px 10px 0; padding:40px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;">-</div> | ||
+ | The symbol <syntaxhighlight lang="pascal" enclose="none">-</syntaxhighlight> (pronounced “minus”) is used to | ||
+ | * indicate the negative sign of a number | ||
+ | * subtract two numbers (using infix notation) | ||
+ | * form the difference of two sets. | ||
+ | <syntaxhighlight lang="pascal"> | ||
+ | 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. | ||
+ | </syntaxhighlight> | ||
+ | Beware: | ||
+ | The result's target of subtractions should be a ''signed'' integer. | ||
+ | If it's not, with [[sRangechecks|<syntaxhighlight lang="pascal" enclose="none">{$rangechecks}}</syntaxhighlight>]] enabled, it will cause an Run-time error. | ||
+ | Otherwise an arithmetically wrong result is produced. | ||
+ | <syntaxhighlight lang="pascal"> | ||
+ | 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. | ||
+ | </syntaxhighlight> | ||
+ | This program prints <syntaxhighlight lang="pascal" enclose="none">4294967295</syntaxhighlight>, which equals to <syntaxhighlight lang="pascal" enclose="none">high(longword)</syntaxhighlight> because of the (unsigned) integer overflow. | ||
− | + | {{Symbols}} | |
− | |||
− | |||
− | |||
− | |||
− | |||
[[Category:Pascal]] | [[Category:Pascal]] | ||
[[Category:Symbols]] | [[Category:Symbols]] |
Revision as of 21:09, 2 April 2018
│
English (en) │
suomi (fi) │
français (fr) │
русский (ru) │
The symbol -
(pronounced “minus”) is used to
- indicate the negative sign of a number
- 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.
Beware:
The result's target of subtractions should be a signed integer.
If it's not, with {$rangechecks}}
enabled, it will cause an Run-time error.
Otherwise 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.
single characters |
|
character pairs |
|