Performs a mathematical calculation on one or more attributes. You can use the interface to set up the expressions, but you can also edit the expressions manually.
You can also use this transformer to evaluate an arbitrary Tcl 8.5.2 expression. It returns the results in a new attribute. The operators permitted in the expressions to be evaluated are a subset of the operators permitted in C expressions. They have the same meaning and precedence as the corresponding C operators.
Expressions usually yield numeric results, such as integer or floating-point values. For example, the expression 8.2 + 6 returns 14.2.
It is easy to build an invalid expression, so you may have to double check that what you build makes sense. Note that the ExpressionEvaluator will fail if a non-numerical attribute value is used in an expression. However, FME will log the offending feature and record which transformer caused the failure.
For more information, see the Arithmetic Editor.
An expression consists of a combination of operands, operators, and parentheses. White space may be used between the operands, operators, and parentheses as it is ignored by the expression processor. Where possible, operands are interpreted as integer values. Integer values may be specified in decimal which is the normal case, in octal if the first character of the operand is 0, or in hexadecimal if the first two characters of the operand are 0x. If an operand does not have one of the integer formats given above, then it will be treated as a floating-point number if possible. Floating-point numbers may be specified in any of the ways accepted by an ANSI-compliant C compiler, except that "f", "F", "l", and "L" suffixes are not permitted in most installations. For example, all of the following are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16. If no numeric interpretation is possible, then an operand is left as a string and only a limited set of operators may be applied to it.
Operands may be specified in any of the following ways:
The valid operators listed below are grouped in decreasing order of precedence:
Function | Description |
---|---|
** |
Exponentiation. Valid for numeric operands only. |
- + ~ ! |
Unary minus, unary plus, bit-wise NOT, logical NOT. None of these operands may be applied to string operands, and bit-wise NOT may be applied only to integers. |
* / % |
Multiply, divide, remainder. None of these operands may be applied to string operands, and remainder may be applied only to integers. The remainder always has the same sign as the divisor and an absolute value smaller than the divisor. |
+ - |
Add and subtract. Valid for any numeric operands. |
<< >> |
Left and right shift. Valid for integer operands only. |
< > <= >= |
Boolean less, greater, less than or equal, and greater than or equal. Each operator produces 1 if the condition is true, 0 otherwise. These operators may be applied to strings as well as numeric operands, in which case string comparison is used. |
== != |
Boolean equal and not equal. Each operator produces a zero/one result. Valid for all operand types. |
& |
Bit-wise AND. Make sure that there is a space between the operand and the operator (e.g., 5 & 3 is valid; 5&3 is not valid). Valid for integer operands only. |
^ |
Bit-wise exclusive OR. Valid for integer operands only. |
| |
Bit-wise OR. Valid for integer operands only. |
&& |
Logical AND. Produces a 1 result if both operands are non-zero, 0 otherwise. Valid for numeric operands only (integers or floating-point). |
|| |
Logical OR. Produces a 0 result if both operands are zero, 1 otherwise. Valid for numeric operands only (integers or floating-point). |
x?y:z |
If-then-else, as in C. If x evaluates to non-zero, then the result is the value of y. Otherwise, the result is the value of z. The x operand must have a numeric value. |
See the C Manual for more details on the results produced by each operator. All binary operators group left-to-right within the same precedence level. For example, the expression:
4*2 < 7
returns 0.
The ExpressionEvaluator supports the following mathematical functions in expressions. Each of these functions invokes the C math library function of the same name. Refer to the C Manual entries for the library functions for details on what they do.
Function | Description |
---|---|
acos(arg) | Returns the arc cosine of arg, in the range [0,pi] radians. Arg should be in the range [-1,1]. |
asin(arg) | Returns the arc sine of arg, in the range [-pi/2,pi/2] radians. Arg should be in the range [-1,1]. |
atan(arg) | Returns the arc tangent of arg, in the range [-pi/2,pi/2] radians. |
atan2(y,x) | Returns the arc tangent of y/x, in the range [-pi,pi] radians. x and y cannot both be 0. |
ceil(arg) | Returns the smallest integer value not less than arg. |
cos(arg) | Returns the cosine of arg, measured in radians. |
cosh(arg) | Returns the hyperbolic cosine of arg. If the result would cause an overflow, an error is returned. |
exp(arg) | Returns the exponential of arg, defined as e**arg. If the result would cause an overflow, an error is returned. |
floor(arg) | Returns the largest integer value not greater than arg. |
fmod(x,y) | Returns the floating-point remainder of the division of x by y. If y is 0, an error is returned. |
hypot(x,y) | Computes the length of the hypotenuse of a right-angled triangle (x*x+y*y). |
log(arg) | Returns the natural logarithm of arg. Arg must be a positive value. |
log10(arg) | Returns the base 10 logarithm of arg. Arg must be a positive value. |
pow(x,y) | Computes the value of x raised to the power y. If x is negative, y must be an integer value. |
sin(arg) | Returns the sine of arg, measured in radians. |
sinh(arg) | Returns the hyperbolic sine of arg. If the result would cause an overflow, an error is returned. |
sqrt(arg) | Returns the square root of arg. Arg must be non-negative. |
tan(arg) | Returns the tangent of arg, measured in radians. |
tanh(arg) | Returns the hyperbolic tangent of arg. |
The ExpressionEvaluator also implements the following functions for conversion between integers and floating-point numbers:
Function | Description |
---|---|
abs(arg) |
Returns the absolute value of arg. Arg may be either integer or floating-point, and the result is returned in the same form. |
double(arg) |
If arg is a floating value, returns arg. Otherwise converts arg to floating point and returns the converted value. |
int(arg) |
If arg is an integer value, returns arg. Otherwise converts arg to integer by truncation and returns the converted value. |
round(arg) |
If arg is an integer value, returns arg. Otherwise converts arg to integer by rounding and returns the converted value. |
All internal computations involving integers are done with C-type long, and all internal computations involving floating-point are done with C-type double. When converting a string to floating-point, exponent overflow is detected and results in an error. For conversion to integer from string, detection of overflow depends on the behavior of some routines in the local C library, so it should be regarded as unreliable. In any case, integer overflow and underflow are generally not reliably detected for intermediate results. Floating-point overflow and underflow are detected to the degree supported by the hardware, which is generally reliable.
Conversion among internal representations for integer, floating-point, and string operands is automatically done as needed. For arithmetical computations, integers are used until some floating-point number is introduced, after which floating-point is used. For example,
5 / 4
returns 1, while
5 / 4.0
and
5 / (4 + 0.0)
both return 1.25. Floating-point values are always returned with a "." or an "e" so that they will not look like integer values. For example,
20.0/5.0
returns “4.0”, not “4”.
Seventeen digits of precision are always used for floating point calculations.
FMEpedia has a good example of the ExpressionEvaluator.
If you are setting more than one attribute, consider using the AttributeCreator, which contains the same functionality.
About Transformer Parameter Options
The ExpressionEvaluator is based on the Tool Command Language (Tcl) expr command.1Tcl and its documentation is copyrighted by the Regents of the University of California, Sun Microsystems, Inc. and other parties. However, the authors have granted permission to any party to reuse and modify the code and documentation, provided the original copyright holders are acknowledged.
arithmetic
Search for samples and information about this transformer on FMEpedia.