# Arithmetic

Xgridfit has a number of elements corresponding to the arithmetic instructions of TrueType. While it is generally best to use Xgridfit's expression syntax for arithmetic, these elements may occasionally come in handy and so are mentioned here. The <set-equal> and <round> elements are especially likely to be useful.

Arithmetic elements generally have one or two attributes for
operands and a `result-to` attribute for the place to
store the result. The `result-to` attribute can be
the name of a variable, an entry in the Control Value Table,
or one of the graphics state variables that can be written
to. If one of the operands is missing, Xgridfit assumes that
the missing value can be found on the run-time stack. If the
`result-to` attribute is missing, Xgridfit attempts
to write the result to the operand that normally appears on
the left in an arithetic expression. Failing that, Xgridfit
issues a warning and generates code that leaves the
result on the stack. The behavior of an arithmetic element
is somewhat different when it is the child of a <formula>
element.

### <add>

Adds `value1` to `value2`. If there is no
`result-to` attribute, Xgridfit attempts to write the
result to `value1`.

### <subtract>

Subtracts `minuend - subtrahend`. If `result-to` is not
specified, Xgridfit attempts to write the result to
`minuend`.

### <multiply>

Multiplies `value1` by `value2`. If there is no
`result-to` attribute, Xgridfit attempts to write the
result to `value1`.

### <divide>

Divides `dividend` by `divisor`. If
`result-to` is not specified, Xgridfit attempts to
write the result to `dividend`.

### <set-equal>

Set `target` (variable, control value, or any of the
graphics state variables that Xgridfit can write to) equal
to `source`, which can be an expression or any number
type that Xgridfit can handle.

### <absolute>

Converts negative to positive numbers; positive numbers stay positive.

### <negate>

Converts positive to negative numbers; negative numbers stay negative.

<!ELEMENT negate EMPTY> <!ATTLIST negate value CDATA #IMPLIED result-to CDATA #IMPLIED>

### <floor>

Yields the greatest integer less than or equal to `value`,
which is either an integer representing 64ths of a pixel or
a fixed-point number. The returned value, if looked at as
an integer, is either zero or a multiple of 64.

### <ceiling>

Yields the smallest integer greater than or equal to
`value`.

### <minimum>

Yields the lesser of the two values `value1` and
`value2`.

### <maximum>

Yields the greater of the two values `value1` and
`value2`.

### <round>

Rounds a distance (expressed by `value`) according to the
current round state, applies whatever correction is
appropriate for the "color" of the distance, and returns the
result.

### <no-round>

Like round, but without the rounding. That is, it may apply a correction for the "color" of the distance, but it will not round the distance.

### <control-value-index>

Assigns the index of a control value to a variable. Use this if you need to get such an index for any reason, since the <set-equal> instruction yields the value, not the index, of a control value.

#### Attributes

- value
- Name of the <control-value> element for which we want the index. No other kind of value is recognized here.
- result-to
- Name of a variable in which to store the control value index. Only a variable is permitted here--not, for example, the name of another control value.

### <formula>

A formula is a block containing arithmetical instruction elements. Within the <formula> the behavior of these elements is modified slightly: when a result-to attribute is missing, they do not attempt to write their result back to one of the values passed to them, but rather leave it on the stack, thus making it available to the following instruction element. Further, while Xgridfit normally issues a warning when an arithmetic element takes a value from the stack or leaves a result on the stack, within a formula this is the expected behavior and so the warning is suppressed. The instructions within the formula should be chained, the result of one operation being used as an argument for the following one; this results in tight code being generated. Example:

<formula result-to="minimum-distance"> <round value="lc-vert-stem"/> <multiply value2="0.8p"/> </formula>

This rounds the control-value "lc-vert-stem," multiplies it by 0.8, and sets the minimum distance in the graphics state to the result. The original entry in the control-value table is unchanged. By contrast, if this <round> element were not the child of a <formula>, the result would be written back to the control-value table.