Skip to content

math: Add options to calculate and format output as hexadecimal

Daniel Franke requested to merge DerDan/cmake:math-hex into master

Extend the command MATH :

math(EXPR <output-variable> <math-expression> [OUTPUT_FORMAT <format>])

  • evaluate the expressions in 64bit
  • evaluate numeric constants in hexadecimal representation (e.g 0x100)
  • check for "divide by 0"
  • improved syntax check
  • optionaly specify output format to be HEXADECIMAL or DECIMAL (default)

For example::

math(EXPR value "100 * 10" ) results in value is set to "1000"

math(EXPR value "100 * 10" OUTPUT_FORMAT DECIMAL) results in value is set to "1000"

math(EXPR value "100 * 0xA" OUTPUT_FORMAT DECIMAL) results in value is set to "1000"

math(EXPR value "100 * 0xA" OUTPUT_FORMAT HEXADECIMAL) results in value is set to "0x3e8"

math(EXPR value "100 / 0") results in value is set to "ERROR", and a "divide by zeror" is reported

math(EXPR value " Y") results in value is set to "ERROR", and a "syntax error" is reported

Edited by Daniel Franke

Merge request reports