Minus: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | 1 | \- | |
| 4 | - | ||
| 5 | - | ||
| 6 | 2 | ||
| 7 | 3 | In [[ASCII]], the character code decimal `45` (or [[Hexadecimal|hexadecimal]] `2D`) is defined to be `-` (hyphen-minus). | |
| 8 | 4 | ||
| 9 | 5 | ## Occurrences | |
| 10 | 6 | ||
| 11 | 7 | The symbol `-` (pronounced “minus”) is used to | |
| 12 | 8 | ||
| 13 | 9 | - indicate the negative sign of a number literal | |
| 14 | 10 | - subtract two numbers (using infix notation) | |
| 15 | 11 | - form the difference of two [[Set|sets]]. | |
| 16 | 12 | ||
| 17 | 13 | ``` pascal | |
| 18 | 14 | program minusDemo(input, output, stderr); | |
| 19 | 15 | ||
| 20 | 16 | var | |
| 21 | 17 | x: longint; | |
| 22 | 18 | g: longint; | |
| 23 | 19 | m: set of (foo, bar); | |
| 24 | 20 | ||
| 25 | 21 | begin | |
| 26 | 22 | // unary operator: negative sign | |
| 27 | 23 | x := -42; // x becomes negative 42 | |
| 28 | 24 | ||
| 29 | 25 | // binary operator: difference of numbers | |
| 30 | 26 | g := 144 - 169; // g becomes negative 25 | |
| 31 | 27 | ||
| 32 | 28 | // binary operator: difference of sets | |
| 33 | 29 | m := [foo, bar] - [bar]; // m becomes {foo} | |
| 34 | 30 | end. | |
| 35 | 31 | ``` | |
| 36 | 32 | ||
| 37 | 33 | Minus is also a unary operator. You can write such stupid expressions as `----+--+--+--8`. | |
| 38 | 34 | ||
| 39 | 35 | ## Operation with numbers | |
| 40 | 36 | ||
| 41 | 37 | Beware: The result's target of subtractions (and sign inversions) should be a *signed* integer. If it's not, with [[sRangechecks|{$rangechecks}]] [[Compiler directive|directive]] enabled, it will possibly cause a [[runtime error|run-time error]]. In any other case an arithmetically wrong result is produced. | |
| 42 | 38 | ||
| 43 | 39 | ``` pascal | |
| 44 | 40 | program faultySubtraction(input, output, stderr); | |
| 45 | 41 | ||
| 46 | 42 | var | |
| 47 | 43 | x, y: longword; | |
| 48 | 44 | ||
| 49 | 45 | begin | |
| 50 | 46 | y := 1; | |
| 51 | 47 | {$push} | |
| 52 | 48 | {$rangechecks off} // otherwise the next expression | |
| 53 | 49 | x := 0 - y; // yields RTE 201 | |
| 54 | 50 | {$pop} | |
| 55 | 51 | ||
| 56 | 52 | writeLn(x); | |
| 57 | 53 | end. | |
| 58 | 54 | ``` | |
| 59 | 55 | ||
| 60 | 56 | This program prints `4294967295`, which equals to `high(longword)` because of the (unsigned) integer overflow. | |
| 61 | 57 | ||
| 62 | 58 | However note, there are situations where negative signs do not necessarily provoke an error. Attempting to store a “negative” zero won't harm: | |
| 63 | 59 | ||
| 64 | 60 | ``` pascal | |
| 65 | 61 | program minusOperations(input, output, stderr); | |
| 66 | 62 | ||
| 67 | 63 | {$rangeChecks on} | |
| 68 | 64 | ||
| 69 | 65 | var | |
| 70 | 66 | n: longword; | |
| 71 | 67 | ||
| 72 | 68 | begin | |
| 73 | 69 | // "negative" zero equals to (sign-less) zero | |
| 74 | 70 | n := 0; | |
| 75 | 71 | n := -n; | |
| 76 | 72 | ``` | |
| 77 | 73 | ||
| 78 | 74 | 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. | |
| 79 | 75 | ||
| 80 | 76 | ``` pascal | |
| 81 | 77 | // intermediate results can be out of range: | |
| 82 | 78 | n := 1; | |
| 83 | 79 | n := n - n - n + 1; | |
| 84 | 80 | end. | |
| 85 | 81 | ``` | |
| 86 | 82 | ||
| 87 | 83 | This whole program will finish with an [`exitCode`](https://www.freepascal.org/docs-html/rtl/system/exitcode.html) of zero (that means “successful” \[actual value might vary among platforms\]). | |
| 88 | 84 | ||
| 89 | 85 | In consequence of the latter mentioned behavior: Integer overflows in the processor's native type can not be caught (in that way). In [[Assembly language|assembler]] you would have access evaluating several status flags, but in [[Pascal]] you do not. Instead you can use [[sOverflowchecks|{$overflowChecks}]] which will evoke a run-time error. | |
| 90 | 86 | ||
| 91 | 87 | ``` pascal | |
| 92 | 88 | program overflowChecksDemo(input, output, stderr); | |
| 93 | 89 | ||
| 94 | 90 | {$overflowChecks on} | |
| 95 | 91 | ||
| 96 | 92 | var | |
| 97 | 93 | n: nativeInt; | |
| 98 | 94 | ||
| 99 | 95 | begin | |
| 100 | 96 | n := high(n); | |
| 101 | 97 | n := n + 1; // will yield RTE 215 | |
| 102 | 98 | end. | |
| 103 | 99 | ``` | |
| 104 | 100 | ||
| 105 | 101 | 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: | |
| 106 | 102 | ||
| 107 | 103 | ``` pascal | |
| 108 | 104 | program overflowAnticipation(input, output, stderr); | |
| 109 | 105 | ||
| 110 | 106 | var | |
| 111 | 107 | n: {$ifdef CPU64} int64 {$else} longint {$endif}; | |
| 112 | 108 | ||
| 113 | 109 | begin | |
| 114 | 110 | // untreated | |
| 115 | 111 | n := high(n); | |
| 116 | 112 | n := n + 1; | |
| 117 | 113 | writeLn(n); // prints low(n) | |
| 118 | 114 | ||
| 119 | 115 | // anticipated: reverse operation and comparison w/ 1st operand | |
| 120 | 116 | n := high(n); | |
| 121 | 117 | if high(n) - 1 < n then | |
| 122 | 118 | begin | |
| 123 | 119 | halt(1); | |
| 124 | 120 | end; | |
| 125 | 121 | n := n + 1; | |
| 126 | 122 | writeLn(n); | |
| 127 | 123 | end. | |
| 128 | 124 | ``` | |
| 129 | 125 | ||
| 130 | 126 | Obviously this won't be as efficient as testing the overflow status flag, and here, without any *re-used* [[Constant|constant]], is even prone to programming errors (consider the situation changing the subtrahend in one line of code only). | |
| 131 | 127 | ||
| 132 | 128 | --- | |
| 133 | 129 | *From the Free Pascal wiki, page "Minus", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
