Next: , Previous: , Up: Programming in M4sugar   [Contents][Index]


8.3.8 Arithmetic computation in M4

The following macros facilitate integer arithmetic operations. Where a parameter is documented as taking an arithmetic expression, you can use anything that can be parsed by m4_eval.

Macro: m4_cmp (expr-1, expr-2)

Compare the arithmetic expressions expr-1 and expr-2, and expand to ‘-1’ if expr-1 is smaller, ‘0’ if they are equal, and ‘1’ if expr-1 is larger.

Macro: m4_list_cmp (list-1, list-2)

Compare the two M4 lists consisting of comma-separated arithmetic expressions, left to right. Expand to ‘-1’ for the first element pairing where the value from list-1 is smaller, ‘1’ where the value from list-2 is smaller, or ‘0’ if both lists have the same values. If one list is shorter than the other, the remaining elements of the longer list are compared against zero.

m4_list_cmp([1, 0],       [1])
⇒0
m4_list_cmp([1, [1 * 0]], [1, 0])
⇒0
m4_list_cmp([1, 2],       [1, 0])
⇒1
m4_list_cmp([1, [1+1], 3],[1, 2])
⇒1
m4_list_cmp([1, 2, -3],   [1, 2])
⇒-1
m4_list_cmp([1, 0],       [1, 2])
⇒-1
m4_list_cmp([1],          [1, 2])
⇒-1
Macro: m4_max (arg, …)

This macro was introduced in Autoconf 2.62. Expand to the decimal value of the maximum arithmetic expression among all the arguments.

Macro: m4_min (arg, …)

This macro was introduced in Autoconf 2.62. Expand to the decimal value of the minimum arithmetic expression among all the arguments.

Macro: m4_sign (expr)

Expand to ‘-1’ if the arithmetic expression expr is negative, ‘1’ if it is positive, and ‘0’ if it is zero.

Macro: m4_version_compare (version-1, version-2)

This macro was introduced in Autoconf 2.53, but had a number of usability limitations that were not lifted until Autoconf 2.62. Compare the version strings version-1 and version-2, and expand to ‘-1’ if version-1 is smaller, ‘0’ if they are the same, or ‘1version-2 is smaller. Version strings must be a list of elements separated by ‘.’, ‘,’ or ‘-’, where each element is a number along with optional case-insensitive letters designating beta releases. The comparison stops at the leftmost element that contains a difference, although a 0 element compares equal to a missing element.

It is permissible to include commit identifiers in version, such as an abbreviated SHA1 of the commit, provided there is still a monotonically increasing prefix to allow for accurate version-based comparisons. For example, this paragraph was written when the development snapshot of autoconf claimed to be at version ‘2.61a-248-dc51’, or 248 commits after the 2.61a release, with an abbreviated commit identification of ‘dc51’.

m4_version_compare([1.1], [2.0])
⇒-1
m4_version_compare([2.0b], [2.0a])
⇒1
m4_version_compare([1.1.1], [1.1.1a])
⇒-1
m4_version_compare([1.2], [1.1.1a])
⇒1
m4_version_compare([1.0], [1])
⇒0
m4_version_compare([1.1pre], [1.1PRE])
⇒0
m4_version_compare([1.1a], [1,10])
⇒-1
m4_version_compare([2.61a], [2.61a-248-dc51])
⇒-1
m4_version_compare([2.61b], [2.61a-248-dc51])
⇒1
Macro: m4_version_prereq (version, [if-new-enough], [if-old = ‘m4_fatal])

Compares version against the version of Autoconf currently running. If the running version is at version or newer, expand if-new-enough, but if version is larger than the version currently executing, expand if-old, which defaults to printing an error message and exiting m4sugar with status 63. When given only one argument, this behaves like AC_PREREQ (see Dealing with Autoconf versions). Remember that the autoconf philosophy favors feature checks over version checks.


Next: Set manipulation in M4, Previous: String manipulation in M4, Up: Programming in M4sugar   [Contents][Index]