Next: , Previous: , Up: gtroff Reference   [Contents][Index]

### 5.3 Expressions

`gtroff` has most arithmetic operators common to other languages:

• Arithmetic: ‘+’ (addition), ‘-’ (subtraction), ‘/’ (division), ‘*’ (multiplication), ‘%’ (modulo).

`gtroff` only provides integer arithmetic. The internal type used for computing results is ‘int’, which is usually a 32-bit signed integer.

• Comparison: ‘<’ (less than), ‘>’ (greater than), ‘<=’ (less than or equal), ‘>=’ (greater than or equal), ‘=’ (equal), ‘==’ (the same as ‘=’).
• Logical: ‘&’ (logical and), ‘:’ (logical or).
• Unary operators: ‘-’ (negating, i.e. changing the sign), ‘+’ (just for completeness; does nothing in expressions), ‘!’ (logical not; this works only within `if` and `while` requests).10 See below for the use of unary operators in motion requests.

The logical not operator, as described above, works only within `if` and `while` requests. Furthermore, it may appear only at the beginning of an expression, and negates the entire expression. Attempting to insert the ‘!’ operator within the expression results in a ‘numeric expression expected’ warning. This maintains compatibility with old versions of `troff`.

Example:

```.nr X 1
.nr Y 0
.\" This does not work as expected
.if (\n[X])&(!\n[Y]) .nop X only
.
.\" Use this construct instead
.if (\n[X]=1)&(\n[Y]=0) .nop X only
```
• Extrema: ‘>?’ (maximum), ‘<?’ (minimum).

Example:

```.nr x 5
.nr y 3
.nr z (\n[x] >? \n[y])
```

The register `z` now contains 5.

• Scaling: `(c;e)`. Evaluate e using c as the default scaling indicator. If c is missing, ignore scaling indicators in the evaluation of e.

Parentheses may be used as in any other language. However, in `gtroff` they are necessary to ensure order of evaluation. `gtroff` has no operator precedence; expressions are evaluated left to right. This means that `gtroff` evaluates ‘3+5*4’ as if it were parenthesized like ‘(3+5)*4’, not as ‘3+(5*4)’, as might be expected.

For many requests that cause a motion on the page, the unary operators ‘+’ and ‘-’ work differently if leading an expression. They then indicate a motion relative to the current position (down or up, respectively).

Similarly, a leading ‘|’ operator indicates an absolute position. For vertical movements, it specifies the distance from the top of the page; for horizontal movements, it gives the distance from the beginning of the input line.

+’ and ‘-’ are also treated differently by the following requests and escapes: `bp`, `in`, `ll`, `lt`, `nm`, `nr`, `pl`, `pn`, `po`, `ps`, `pvs`, `rt`, `ti`, `\H`, `\R`, and `\s`. Here, leading plus and minus signs indicate increments and decrements.

See Setting Registers, for some examples.

Escape: \B'anything'

Return 1 if anything is a valid numeric expression; or 0 if anything is empty or not a valid numeric expression.

Due to the way arguments are parsed, spaces are not allowed in expressions, unless the entire expression is surrounded by parentheses.

Next: , Previous: , Up: gtroff Reference   [Contents][Index]