cmake/Help/command/math.rst

40 lines
1.3 KiB
ReStructuredText
Raw Normal View History

2014-08-03 19:52:23 +02:00
math
----
2019-11-11 23:01:05 +01:00
Evaluate a mathematical expression.
2014-08-03 19:52:23 +02:00
2019-11-11 23:01:05 +01:00
.. code-block:: cmake
2014-08-03 19:52:23 +02:00
2019-11-11 23:01:05 +01:00
math(EXPR <variable> "<expression>" [OUTPUT_FORMAT <format>])
2014-08-03 19:52:23 +02:00
2019-11-11 23:01:05 +01:00
Evaluates a mathematical ``<expression>`` and sets ``<variable>`` to the
2020-02-01 23:06:01 +01:00
resulting value. The result of the expression must be representable as a
2023-07-02 19:51:09 +02:00
64-bit signed integer. Floating point inputs are invalid e.g. ``1.1 * 10``.
Non-integer results e.g. ``3 / 2`` are truncated.
2019-11-11 23:01:05 +01:00
The mathematical expression must be given as a string (i.e. enclosed in
double quotation marks). An example is ``"5 * (10 + 13)"``.
2016-10-30 18:24:19 +01:00
Supported operators are ``+``, ``-``, ``*``, ``/``, ``%``, ``|``, ``&``,
2019-11-11 23:01:05 +01:00
``^``, ``~``, ``<<``, ``>>``, and ``(...)``; they have the same meaning
as in C code.
2021-09-14 00:13:48 +02:00
.. versionadded:: 3.13
Hexadecimal numbers are recognized when prefixed with ``0x``, as in C code.
.. versionadded:: 3.13
The result is formatted according to the option ``OUTPUT_FORMAT``,
where ``<format>`` is one of
``HEXADECIMAL``
Hexadecimal notation as in C code, i. e. starting with "0x".
``DECIMAL``
Decimal notation. Which is also used if no ``OUTPUT_FORMAT`` option
is specified.
2018-10-28 12:09:07 +01:00
2019-11-11 23:01:05 +01:00
For example
2018-10-28 12:09:07 +01:00
2019-11-11 23:01:05 +01:00
.. code-block:: cmake
2018-10-28 12:09:07 +01:00
2019-11-11 23:01:05 +01:00
math(EXPR value "100 * 0xA" OUTPUT_FORMAT DECIMAL) # value is set to "1000"
math(EXPR value "100 * 0xA" OUTPUT_FORMAT HEXADECIMAL) # value is set to "0x3e8"