Evaluate

Interprets basic operations supported by some of the built-in types of C#, and also methods from some of the built-in classes of C#, like the Math class.

Command availability: IBM RPA SaaS and IBM RPA on premises

Description

Interprets basic operations supported by some of the built-in types of C#, and also methods from some of the built-in classes of C#, like the Math class. The following list defines the supported operations:

  • Binary arithmeric operations
  • Comparison operations
  • Binary conditional logical operations
  • Equality operations

The following list defines the supported C# types:

Name Syntax
Boolean bool
Object object
Char char
Decimal decimal
Double double
Single float
Integer int
UInt32 uint
IntPtr nint
UIntPtr nuint
Int64 long
UInt64 ulong
Int16 short
UInt16 ushort
DateTime DateTime
Guid Guid

You can use IBM RPA variables on your operations. By default, IBM RPA converts your Number variables to C# decimals, if the expression uses basic arithmetic operators, or to C# doubles, if the expression uses other operators. You can convert IBM RPA variables to the supported C# types with explicit conversion syntax, also called casting: (<c#_type>)${rpa_variable}. The following expression exemplifies casting in a valid evaluate expression:

Math.Round(${var1}, (int)${var2})

The following list defines the Math methods that allow casting:

  • BigMul
  • Ceiling
  • Floor
  • Round
  • Truncate

🛈 Important: The Evaluate command isn't a calculator tool. If you use it as a calculator, keep in mind that the results might not be accurate. To understand how the command evaluates, see Expressions. See Limitations for possible rounding errors and number precision details.

Limitations

  • It is possible that you get round-off errors because of the way computers represent numbers. A round-off error is an error that occurs with floating-point numbers. To avoid such errors, use the Math.Round() operation. Rounding a number might produce a value that is easier to deal with than the actual value, but might also lose accuracy. For example, the decimal value of 4.5 rounds to 4.
  • The integer number types have the following restrictions:
Type Minimum integer value Maximum integer value Integer Precision
Decimal -79,228,162,514,264,337,593,543,950,335 79,228,162,514,264,337,593,543,950,335 28 digits
Double -9,007,199,254,740,992 9,007,199,254,740,992 15 digits
Integer -2,147,483,648 2,147,483,647 10 digits
UInt32 0 4,294,967,295 10 digits
IntPtr Architecture-dependent:
32-bit: -2,147,483,648
64-bit: -9,223,372,036,854,775,808
Architecture-dependent:
32-bit: 2,147,483,647
64-bit: 9,223,372,036,854,775,807
32-bit: 10 digits
64-bit: 19 digits
UIntPtr 0 Architecture-dependent:
32-bit: 4,294,967,295
64-bit: 18,446,744,073,709,551,615
32-bit: 10 digits
64-bit: 20 digits
Int64 -9,223,372,036,854,775,808 9,223,372,036,854,775,807 19 digits
UInt64 0 18,446,744,073,709,551,615 20 digits
Int16 -32,768 32,767 5 digits
UInt16 0 65,535 5 digits
  • The floating-point number types follow the IEEE 754 standard.

Script syntax

evaluate --expression(String) (Numeric)=value

Input parameter

Script Designer Required Accepted types Description
expression Expression Required Text, Number, Money, Date Time, Date, Time Span, Boolean Supports mathematical expressions, logical expressions, spring expressions, and expressions with compound conditions.

It also evaluate reference to variables in answers returned by the Answer Question (answerQuestion) and Bot Ask and Answer (botAnswerQuestion) commands. A reference to a variable is any text that follows the ${variableName} pattern, where the variable is replaced by a value that is defined by you on the script.

Expressions

Mathematical expressions

When using expressions in plain text, for example: "1.2 + 3.4", the command will consider the number type as it perceives it. If the number has a decimal part, it will be considered as a double. Otherwise, it will be considered as an integer.

When using expressions that have variables, for example: "${firstNumber} + ${secondNumber}", the command will consider the number type regarding the operation. If the expression only contains basic math operations (+ - * / %), it will consider numbers as decimals. Otherwise, the numbers will be considered as doubles.

Money type has a specific formatting rule, so it may require the double casting regarding the value, that is, on the values that don’t have decimal part.

The following list show all .NET Math functions supported:

Operation Syntax Description
Abs Math.Abs(double, int) Returns the absolute value of a double-precision floating-point or integer number.
Acos Math.Acos(double) Returns the angle whose cosine is the specified number.
Asin Math.Asin(double) Returns the angle whose sine is the specified number.
Atan Math.Atan(double) Returns the angle whose tangent is the specified number.
Ceiling Math.Ceiling(double) Returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number.
Cos Math.Cos(double) Returns the cosine of the specified angle.
Cosh Math.Cosh(double) Returns the hyperbolic cosine of the specified angle.
Exp Math.Exp(double) Returns a number raised to the specified power.
Floor Math.Floor(double) Returns the largest integral value less than or equal to the specified double-precision floating-point number.
IEEERemainder Math.IEEERemainder(double) Returns the remainder resulting from the division of a specified number by another specified number.
Log Math.Log(double) Returns the natural (base n) logarithm of a specified number.
Log10 Math.Log10(double) Returns the base 10 logarithm of a specified number.
Max Math.Max(double, int) Returns the larger of two numbers. The types can be: Double or Integer.
Min Math.Min(double, int) Returns the smaller of two numbers. The types can be: Double or Integer.
Pow Math.Pow(double, double) Returns a specified number raised to the specified power.
Round Math.Round(double) Rounds a floating-point value to the nearest integral value, and rounds midpoint values to the nearest even number.
Sign Math.Sign(double, int) Returns an integer that indicates the sign of a double-precision floating-point or integer number.
Sin Math.Sin(double) Returns the sine of the specified angle.
Sinh Math.Sinh(double) Returns the hyperbolic sine of the specified angle.
Tan Math.Tan(double) Returns the tangent of the specified angle.
Tanh Math.Tanh(double) Returns the hyperbolic tangent of the specified angle.
Truncate Math.Truncate(double) Calculates the integral part of a specified double-precision floating-point number.

The following list show all .NET Math functions with casting supported:

Operation Type Syntax Casting Description
BigMul Numeric Math.BigMul(int, int) Math.BigMul((int)${var1}, (int)${var2}) Produces the full product of two integer numbers.
Round (3) Numeric Math.Round(double, int) Math.Round(${var1}, (int)${var2}) Rounds a floating-point value to a specified number of fractional digits, and rounds midpoint values to the nearest even number.
Round (3) ¹ Money Math.Round(double, int) Math.Round((double)${var1}, ${var2}) Rounds a double-precision floating-point value to the nearest integral value, and rounds midpoint values to the nearest even number.
Ceiling ¹ Money Math.Ceiling(double) Math.Ceiling((double)${var1}) Returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number.
Floor ¹ Money Math.Floor(double) Math.Floor((double)${var1}) Returns the largest integral value less than or equal to the specified double-precision floating-point number.
Truncate ¹ Money Math.Truncate(double) Math.Truncate((double)${var1}) Calculates the integral part of a specified double-precision floating-point number.
Round ¹ Money Math.Round(double) Math.Round((double)${var1}) Rounds a double-precision floating-point value to the nearest integral value, and rounds midpoint values to the nearest even number.

¹ It depends on the value set to the variable. It is more effective to do the casting when you don’t know what value to expect.

String expressions

When using String expressions, the variables must be between double quotes. For example: "${testVariable}".EndsWith("${e}"). The syntax of the operations only works with "String" and "string" for the library name.

The following list show all operations supported for String expressions:

Operation Syntax Description
Compare String.Compare(string, string) Compares two specified String objects and returns an integer that indicates their relative position in the sort order.
CompareOrdinal String.CompareOrdinal(string, string) Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string.
CompareTo "${value}".CompareTo(string) Compares this instance with a specified String object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified string.
Contains "${value}".Contains(string) Returns a value indicating whether a specified substring occurs within this string.
EndsWith "${value}".EndsWith(string) Determines whether the end of this string instance matches the specified string.
Equals "${value}".Equals(string) Determines whether two specified String objects have the same value.
IndexOf "${value}".IndexOf(string) Reports the zero-based index of the first occurrence of the specified string in this instance.
IsNormalized "${value}".IsNormalized() Indicates whether this string is in Unicode normalization form C.
LastIndexOf "${value}".LastIndexOf(string, int) Reports the zero-based index position of the last occurrence of a specified string within this instance.
StartsWith "${value}".StartsWith(string) Determines whether the beginning of this string instance matches the specified string.
IsNullOrWhiteSpace String.IsNullOrWhiteSpace(string) Indicates whether a specified string is null, empty, or consists only of white-space characters.
IsNullOrEmpty String.IsNullOrEmpty(string) Indicates whether the specified string is null or an empty string.

Boolean expressions and compound conditions

You can use the supported operators to form compound conditions with operations from other libraries, such as math or string libraries.

The following list show all operators supported for Boolean expressions and Compound conditions:

Operator Syntax Description
AND && Returns true if both operands are true.
Equal == Compares their operands for strict equality or inequality.
Greater > Compares if one operand is greater than another.
Less < Compares if one operand is less than another.
Greater Than or Equal >= Compares if one operand is greater than or equal to another.
Less Than or Equal <= Compares if one operand is less than or equal to another.
Negate ! Determines the oposite value.
Not Equal != Returns true if the operands don't have the same value; otherwise, it returns false.
OR || Returns true if any of the operands are true.
XOR ^ Returns true if the operands are opposites; otherwise, it returns false.

Other expressions

The command also evaluates DateTime and TimeSpan values. The variables must be between double quotes, and it only works with expressions that return Booleans and Numbers. See Example 4 for an example on how the command deal with these libraries.

Fuzzy variables are also supported. You can convert the Fuzzy type variables to a Numeric type after the Fuzzy operation with this command.

Output parameter

Script Designer Accepted types Description
value Result Number, Text, Boolean Returns the result of the calculated expression if the output is a string, the command will do variable replacement. Otherwise, it will attempt to evaluate the expression and parse it to the desired output.

Example

Example 1: A numeric variable evaluatedExpression is defined for storing the result of "10 * (5 + 5)", displaying it on the IBM RPA Studio console with the Log Message command.

defVar --name evaluatedExpression --type Numeric
// Evaluates the mathematical expression set.
evaluate --expression "10*(5+5)" evaluatedExpression=value
// Prints the result of the expression.
logMessage --message "${evaluatedExpression}" --type "Info"

Example 2: A numeric variable evaluatedExpression is defined for storing the result of "${numberExpression1} * ${numberExpression2}". Unlike the previous example, variables with numeric values ​​are used.

defVar --name evaluatedExpression --type Numeric
defVar --name numberForCalculation1 --type Numeric --value 10
defVar --name numberForCalculation2 --type Numeric --value 5
// Evaluates the mathematical expression set.
evaluate --expression "${numberForCalculation1} * ${numberForCalculation2}" evaluatedExpression=value
// Prints the result of the expression.
logMessage --message "${evaluatedExpression}" --type "Info"

Example 3: In this example, two variables of type String are set, and the command will evaluate an expression with these variables.

defVar --name evaluatedExpression --type Text
defVar --name firstString --type Test --value a
defVar --name secondString --type Text --value b
// Evaluates the string expression set.
evaluate --expression "${firstString} ${secondString}" evaluatedExpression=value
// Prints the result of the expression.
logMessage --message "${evaluatedExpression}" --type "Info"

Example 4: In this example, one variable of each of the types: Date, DateTime and TimeSpan are set, the command will evaluate each variable individually and print its result.

defVar --name result --type Numeric
defVar --name d --type Date --value "2022-08-01"
defVar --name dt --type DateTime --value "2022-08-01 03:00:00"
defVar --name t --type TimeSpan --value "05:40:32"
// Evaluates the expression set with the variable of type DateTime.
evaluate --expression "DateTime.Parse(\"${dt}\").Hour" result=value
// Prints the result of the expression.
logMessage --message "Hour from \"${dt}\": ${result}" --type "Info"
// Evaluates the expression set with the variable of type Date.
evaluate --expression "DateTime.Parse(\"${d}\").Month" result=value
// Prints the result of the expression.
logMessage --message "Month from \"${d}\": ${result}" --type "Info"
// Evaluates the expression set with the variable of type TimeSpan.
evaluate --expression "TimeSpan.Parse(\"${t}\").Minutes" result=value
// Prints the result of the expression.
logMessage --message "Minutes from \"${t}\": ${result}" --type "Info"

Example 5: In this example, two expressions are set and connected through the AND operator. The command will evaluate the expression according to the operador.

defVar --name evaluatedExpression --type Text
defVar --name firstString --type Test --value a
defVar --name secondString --type Text --value b
defVar --name firstNumber --type Numeric --value 1
defVar --name secondNumbert --type Numeric --value 2
// Evaluates the string expression set.
evaluate --expression "${firstString}".StartsWith("${secondString}") && ${firstNumber} > Math.Min(${firstNumber},${secondNumber}))" evaluatedExpression=value
// Prints the result of the expression.
logMessage --message "${evaluatedExpression}" --type "Info"

Example 6: In this example, the command evaluates two expressions. The first expression does not use the Math.Round() which causes the round-off error, while the second expression uses the Math.Round() method, which produces a easier number to deal with.

defVar --name number1 --type Numeric --value 11
defVar --name number2 --type Numeric --value 2
defVar --name expressionResult --type Numeric
evaluate --expression "${number1}/${number2}" expressionResult=value
logMessage --message " Result of the expression without rounding: ${expressionResult}" --type "Info"
evaluate --expression "Math.Round(${number1}/${number2})" expressionResult=value
logMessage --message " Result of the expression without rounding: ${expressionResult}" --type "Info"

Example 7: In this example, after a Fuzzy operation, this command is used to get the numeric value of the tip variable.

defVar --name low --type FuzzySet
defVar --name medium --type FuzzySet
defVar --name good --type FuzzySet
defVar --name serviceQuality --type Fuzzy
defVar --name foodQuality --type Fuzzy
defVar --name littleTip --type FuzzySet
defVar --name reasonableTip --type FuzzySet
defVar --name generousTip --type FuzzySet
defVar --name tip --type Fuzzy
defVar --name tipValue --type Numeric
beginFuzzy --intervals 1000
	fuzzySet --function "Trapezoidal" --shape "Number" --edge "Right" --m1 0 --m2 5 low=value
	fuzzySet --function "Trapezoidal" --shape "Triangular" --m1 3 --m2 5 --m3 7 medium=value
	fuzzySet --function "Trapezoidal" --shape "Number" --edge "Left" --m1 5 --m2 10 good=value
	fuzzyVariable --left 0 --right 10 --labels "${low} ${medium} ${good}" serviceQuality=value
	fuzzyVariable --left 0 --right 10 --labels "${low} ${medium} ${good}" foodQuality=value
	fuzzySet --function "Trapezoidal" --shape "Number" --edge "Right" --m1 0 --m2 12.5 littleTip=value
	fuzzySet --function "Trapezoidal" --shape "Triangular" --m1 6.25 --m2 12.5 --m3 18.75 reasonableTip=value
	fuzzySet --function "Trapezoidal" --shape "Number" --edge "Left" --m1 12.5 --m2 25 generousTip=value
	fuzzyVariable --left 0 --right 25 --labels "${littleTip} ${reasonableTip} ${generousTip}" tip=value
	
	//The food was rancid or the service was poor.
	fuzzyRule --name "Terrible food or service" --condition "${foodQuality} IS ${low} OR ${serviceQuality} IS ${low}" --variable ${tip} --set ${littleTip}
	
	//The service was reasonable.
	fuzzyRule --name "Average service" --condition "${serviceQuality} IS ${medium}" --variable ${tip} --set ${reasonableTip}
	
	//The food was delicious or the service was good.
	fuzzyRule --name "Great Food or Service" --condition "${foodQuality} IS ${good} OR ${serviceQuality} IS ${good}" --variable ${tip} --set ${generousTip}
endFuzzy
evaluate --expression "${tip}" tipValue=value