math.rst 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. Hexadecimal numbers are recognized when prefixed with ``0x``, as in C code.
  15. The result is formatted according to the option ``OUTPUT_FORMAT``,
  16. where ``<format>`` is one of
  17. ``HEXADECIMAL``
  18. Hexadecimal notation as in C code, i. e. starting with "0x".
  19. ``DECIMAL``
  20. Decimal notation. Which is also used if no ``OUTPUT_FORMAT`` option
  21. is specified.
  22. For example
  23. .. code-block:: cmake
  24. math(EXPR value "100 * 0xA" OUTPUT_FORMAT DECIMAL) # value is set to "1000"
  25. math(EXPR value "100 * 0xA" OUTPUT_FORMAT HEXADECIMAL) # value is set to "0x3e8"