Class CX_FORMULA - Examples

 

Simple Calculations

Formula equivalent to Comment
 a + b  (a + b) a and b are dynamic data fields
 0.43cm * (a + b)  
 35.77m a formula must consist of one single constant only
 2.0 * sin(phi) phi be a dynamic data field
 (a + b) * (a + b)  pow(a + b, 2) first version is better!
 - a * b  b  *  - a minus as a algebraic sign, not as a mathematical operator
 owner.value + value   with access path
 Previous(::Day(24,12) ,6) - 28 Function Day is bound to an object CX_DATE, the formula calculates for this specified year the date for "Totensonntag". 
This is an example for an external (day) and an internal function call (Previous), 
 Previous(Day(Date("heute"), 24,12), 6) - 28 Different to the previous example, the function Day is called for the date "today" - and is therefore an internal function call - and we receive the date for "Totensonntag" this year.

  

Calculations with Conditioned Expressions

Formula equivalent to Comment
 if (a < b) a else b  min(a, b)  
 x + if (a = b) 1.0 else 2.0  if (a = b) a + 1 else x + 2  
 x * if (a > 0.0 & a < 10.0) 2.0 else 1.0  if (a > 0.0 & a < 10.0) 2.0*x else x  
 if (has(a)) a + b else b   checks existence of the data field a
 if (if (has(a)) b > a else b > 0) 24.5$ else 12.5$ if (b > if (has(a) a else 0) 24.5$ else 12.5$  

 

The Formula Defines a Condition (Grade)

Formula equivalent to Comment
 a > b  b <= a Result is a CX_BOOLEAN object
 a = 4.0 & b > 10.0  ! (a != 4.0 | b <= 10.0)
 true  
 false  
 if (has(a)) a < 12.0m else false  

 

Local Variable and Value Assignment

Formula equivalent to Comment
 local tmp1, tmp2;
 tmp1 := (a + b) * (a - b);
 tmp2 := (a + b) * (a + b);
 1 / tmp1 + 1 / tmp2
 1 / ((a + b) * (a - b)) + 1 /  ((a + b) * (a + b)) complex formulas become more clear...
 local tmp;
 tmp := a*a - b;
 tmp*tmp*tmp + tmp*tmp + tmp
(a*a - b)*(a*a - b)*(a*a - b) - (a*a - b)*(a*a - b) -  a*a - b and terms, that repeat often are calculated only once 
 pow(a*a - b, 3) + pow(a*a - b, 2) + a * a - b

 

Formulas with Fields

Faulty Formula Why Faulty? ...Correct is
 a + 23,5m numeric constant always with a decimal point  a + 23.5m
 (a + b)(a - b) the common mathematic way of display is not supported, the multiplication operator has to be indicated explicitly  (a + b) * (a - b)
 t * 25.5m/s measuring unit m/s must be inside [ ]   t * 25.5[m/s]
 t ** 2 there is no operator for potentiation!  t * t
 a ** 3.27  pow(t, 3.27)
 + a * b only minus is accepted as another algebraic sign; the plus sign can only be used as an addition operator  a * b
 if (a < b) 25.4 no result for a >= b  if (a < b) 25.4 else 0.0
 if a < b   25.4 else 0.0 parnthesis after if are missing  if (a < b) 25.4 else 0.0
 (a + b).c access path starts with provisional result  (cannot be expressed!)
 (a + b).Foo()  Foo(a + b)
 if (a == b) x else y operator for equal is = and not == as in C / C++  if (a = b) x else y
 local tmp;
 tmp = (a + b) / (a - b);
 sinh(tmp)
operator for value assignment is := and not = as in C / C++  local tmp;
 tmp := (a + b) / (a - b);
 sinh(tmp)
 local tmp1, tmp2;
 tmp1 := tmp2 * (a - b);
 tmp2 := a * (a - 1) * (a - 2);
 tmp1 + tmp2
the local variable tmp2 is already used before value assignment  local tmp1, tmp2;
 tmp1 := a * (a - 1) * (a - 2);
 tmp2 := tmp1 * (a - b);
 tmp1 + tmp2
 a + b;  a - b correct but redundant, since a + b doesn't need to be calculated!  a - b
ma = if (a < b) Stahl else
  "Sympal verzinkt"
Although ma (dynamic data field "material type") refers to a transformation table, this connection to the constants is too indirect ma = if (a < b) ma::steel else
  ma::"Sympal galvanized"
if (a < b) ma = steel else ma = "Sympal galvanized"