[%CFEngine_function_prototype(mode, options, expression)%]

Description: Returns expression evaluated according to mode and options. Currently only the math mode with infix option is supported for evaluating traditional math expressions.

All the math is done with the C double type internally. The results are returned as a string

The supported infix mathematical syntax, in order of precedence, is:

The numbers can be in any format acceptable to the C scanf function with the %lf format specifier, followed by the k, m, g, t, or p SI units. So e.g. -100 and 2.34m are valid numbers.

In addition, the following constants are recognized:

The following functions can be used, with parentheses:

[%CFEngine_function_attributes(mode, options, expression)%]

Example:

[%CFEngine_include_snippet(eval.cf, #+begin_src cfengine3, .*end_src)%]

Output:

2013-09-14T08:34:16-0400 info: eval error: expression could not be parsed (input 'x') 2013-09-14T08:34:16-0400 info: eval error: expression could not be parsed (input '+ 200') 2013-09-14T08:34:16-0400 info: eval error: expression could not be parsed (input '- - -') 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('x') = '' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('+ 200') = '' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('floor(3.4)') = '3.000000' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('sqrt(0.2)') = '0.447214' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('2 + 3 - 1') = '4.000000' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('sin(20)') = '0.912945' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('200 - 100') = '100.000000' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('20 % 3') = '2.000000' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('ceil(3.5)') = '4.000000' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('200 + 100') = '300.000000' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('pi') = '3.141593' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('- - -') = '' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('-3.400000 == -3.400001') = '0.000000' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('3 / 0') = 'inf' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('e') = '2.718282' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('cos(20)') = '0.408082' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('') = '0.000000' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('-1^2.1') = '-nan' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('abs(-3.4)') = '3.400000' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('-3.4 == -3.4') = '1.000000' 2013-09-14T08:34:16-0400 notice: R: math/prefix eval('3^3') = '27.000000'