math.rst 1.3 KB

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