manpagez: man pages & more
info autoconf
Home | html | info | man

File: autoconf.info,  Node: Number processing Macros,  Next: Set manipulation Macros,  Prev: Text processing Macros,  Up: Programming in M4sugar

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’.  Any other numeric
parameter should consist of an optional sign followed by one or more
decimal digits; it is treated as a decimal integer.

   Macros that expand to a number do so as either ‘0’, or an optional
‘-’ followed by a nonzero decimal digit followed by zero or more decimal
digits.

   Due to ‘m4’ limitations, arithmetic expressions and numeric
parameters should use only numbers that fit into a 32-bit signed
integer.

 -- 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 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 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 ‘1’
     VERSION-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’ (*note Versioning::).
     Remember that the autoconf philosophy favors feature checks over
     version checks.

© manpagez.com 2000-2025
Individual documents may contain additional copyright information.