math.rst 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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.
  9. The mathematical expression must be given as a string (i.e. enclosed in
  10. double quotation marks). An example is ``"5 * (10 + 13)"``.
  11. Supported operators are ``+``, ``-``, ``*``, ``/``, ``%``, ``|``, ``&``,
  12. ``^``, ``~``, ``<<``, ``>>``, and ``(...)``; they have the same meaning
  13. as in C code.
  14. .. versionadded:: 3.13
  15. Hexadecimal numbers are recognized when prefixed with ``0x``, as in C code.
  16. .. versionadded:: 3.13
  17. The result is formatted according to the option ``OUTPUT_FORMAT``,
  18. where ``<format>`` is one of
  19. ``HEXADECIMAL``
  20. Hexadecimal notation as in C code, i. e. starting with "0x".
  21. ``DECIMAL``
  22. Decimal notation. Which is also used if no ``OUTPUT_FORMAT`` option
  23. is specified.
  24. For example
  25. .. code-block:: cmake
  26. math(EXPR value "100 * 0xA" OUTPUT_FORMAT DECIMAL) # value is set to "1000"
  27. math(EXPR value "100 * 0xA" OUTPUT_FORMAT HEXADECIMAL) # value is set to "0x3e8"