math.rst 1.0 KB

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