• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1math
2----
3
4Evaluate a mathematical expression.
5
6.. code-block:: cmake
7
8  math(EXPR <variable> "<expression>" [OUTPUT_FORMAT <format>])
9
10Evaluates a mathematical ``<expression>`` and sets ``<variable>`` to the
11resulting value.  The result of the expression must be representable as a
1264-bit signed integer.
13
14The mathematical expression must be given as a string (i.e. enclosed in
15double quotation marks). An example is ``"5 * (10 + 13)"``.
16Supported operators are ``+``, ``-``, ``*``, ``/``, ``%``, ``|``, ``&``,
17``^``, ``~``, ``<<``, ``>>``, and ``(...)``; they have the same meaning
18as in C code.
19
20.. versionadded:: 3.13
21  Hexadecimal numbers are recognized when prefixed with ``0x``, as in C code.
22
23.. versionadded:: 3.13
24  The result is formatted according to the option ``OUTPUT_FORMAT``,
25  where ``<format>`` is one of
26
27  ``HEXADECIMAL``
28    Hexadecimal notation as in C code, i. e. starting with "0x".
29  ``DECIMAL``
30    Decimal notation. Which is also used if no ``OUTPUT_FORMAT`` option
31    is specified.
32
33For example
34
35.. code-block:: cmake
36
37  math(EXPR value "100 * 0xA" OUTPUT_FORMAT DECIMAL)      # value is set to "1000"
38  math(EXPR value "100 * 0xA" OUTPUT_FORMAT HEXADECIMAL)  # value is set to "0x3e8"
39