Whole document tree
Macros for doing arithmetic
Integer arithmetic is included in Decrement and increment operators
Increment and decrement of integers are supported using the builtins
incr(number) decr(number) which expand to the numerical value of number, incremented, or decremented, respectively, by one. incr(4) =>5 decr(7) =>6
The builtin macros Evaluating integer expressions
Integer expressions are evaluated with eval(expression, opt radix, opt width) which expands to the value of expression. Expressions can contain the following operators, listed in order of decreasing precedence.
All operators, except exponentiation, are left associative.
Note that many Numbers without special prefix are given decimal. A simple `0' prefix introduces an octal number. `0x' introduces an hexadecimal number. `0b' introduces a binary number. `0r' introduces a number expressed in any radix between 1 and 36: the prefix should be immediately followed by the decimal expression of the radix, a colon, then the digits making the number. For any radix, the digits are `0', `1', `2', .... Beyond `9', the digits are `a', `b' ... up to `z'. Lower and upper case letters can be used interchangeably in numbers prefixes and as number digits.
Parentheses may be used to group subexpressions whenever needed. For the
relational operators, a true relation returns
Here are a few examples of use of eval(-3 * 5) =>-15 eval(index(`Hello world', `llo') >= 0) =>1 define(`square', `eval(($1)**2)') => square(9) =>81 square(square(5)+1) =>676 define(`foo', `666') => eval(`foo'/6) error-->51.eval:14: m4: Bad expression in eval: foo/6 => eval(foo/6) =>111
As the second to last example shows,
If radix is specified, it specifies the radix to be used in the
expansion. The default radix is 10. The result of eval(666, 10) =>666 eval(666, 11) =>556 eval(666, 6) =>3030 eval(666, 6, 10) =>0000003030 eval(-666, 6, 10) =>-000003030 Take note that radix cannot be larger than 36.
The builtin macro Go to the first, previous, next, last section, table of contents. |