Perform Mathematical Calculations In Rules

It is possible to perform a variety of mathematical calculations using non-Boolean number and currency attributes. These operations include:

  • standard arithmetic calculations (for example, addition, subtraction, multiplication, division)
  • mathematical expressions (for example, square root, round, truncation, remainder)
  • trigonometric calculations (for example, sine, cosine, tangent)

Numerical functions are used to perform these calculations. For the full list of supported functions, see Numerical functions.

The Loan Advisor example policy model that is installed with Policy Modeling contains many rules using arithmetic calculations. For example:

the payment interest amount = the payment balance * the monthly rate

the loan amount = the purchase price - the down payment

the loan duration in years = the loan duration / 12

Where multiple mathematical operations are included in a single rule, the standard mathematical preference is applied to operators in the absence of parentheses (that is, division, multiplication, addition, subtraction). In other words, these two rules are equivalent:

the loan to value ratio = the loan amount / the purchase price * 100

the loan to value ratio = (the loan amount / the purchase price) * 100

Tip: Using parentheses can make the calculation easier to read.

For more complex calculations, separating your rule into several steps can be beneficial. For example, while you could write:

the total payable = Maximum((the order total - Trunc((the order total * the discount percentage), 2) - the coupon amount),0)

breaking this down into four separate rules makes the calculations easier to understand and easier to test:

the total payable = Maximum(the total order amount including discounts and coupons, 0)

the total order amount including discounts and coupons = the order total - the order discount in whole dollars - the coupon amount

the order discount in whole dollars = Trunc(the order discount, 2)

the order discount = the order total * the discount percentage

Note: Be sure to use the exact syntax for the numerical functions as specified in the Function Reference, including spacing and parentheses. It is particularly important to include spaces either side of an mathematical operator. This is because if there is a letter or number on both sides of the operator, then the operator will be considered as part of the attribute text and will not function as an operator.

Examples of rules performing mathematical calculations, with inputs and outputs, are given in the table below.

Table 1. Examples of the use of the numerical functions
Function Example rule Inputs Outputs
Addition

the household's total gross monthly income = the household's gross monthly earned income + the household's gross monthly unearned income

the household's gross monthly earned income: 1000

the household's gross monthly unearned income: 500

the household's total gross monthly income = 1500
Subtraction

the household's adjusted monthly income = the household's total gross monthly income - the household's monthly deductions

the household's total gross monthly income: 5000

the household's monthly deductions: 750

the household's adjusted monthly income = 4250
Multiplication

the default down payment amount = the purchase price * 0.2

the purchase price: 15000

the default down payment amount = 3000
Division

the weekly loan repayment amount = the loan amount / 1000

the loan amount: 90000

the weekly loan repayment amount = 90
Number

the number = Number(the number text)

the number text: 15 the number = 15
Maximum

the repayment amount = Maximum(50, the payment balance * 0.1)

the payment balance: 600

the repayment amount = 60
Minimum

the work-related child care costs = the lesser of the estimated annual child care expenses and the calculated amount of work-related child care costs

the estimated annual child care expenses: 8000

the calculated amount of work-related child care costs: 7500

the work-related child care costs = 7500
Remainder (modulo)

the remaining number = Remainder(the first number, the second number)

the first number: 10

the second number: 3

the remaining number = 1
Exponentiation (xy)

the result = Xy(the value,3)

the value: 5 the result = 125
Mathematical Constant (ex)

the result = Ex(the value)

the value: 0.3527 the result = 1.42290420813407
Absolute value

the result = Abs(the value)

the value: -80 the result = 80
Natural Logarithm

the result = Ln(the value)

the value: 0.3527 the result = -1.04213744174013
Logarithm Base

the result = Log(the value)

the value: 0.3527 the result = -0.45259454033251
Square Root

the result = Sqrt(the value)

the value: 64 the result = 8
Round

the result = Round(the value,3)

the value: 2.45678

the result = 2.457
Truncation

the result = Trunc(the value,1)

the value: 64.4657

the result = 64.4
Sine

the result = Sin(the value)

the value: 0.3527 the result = 0.345432860836779
Cosine

the result = Cos(the value)

the value: 0.3527 the result = 0.938443465880667
Tangent

the result = Tan(the value)

the value: 0.3527 the result = 0.368091284553421
Inverse Sine

the result = Asin(the value)

the value: 0.3527 the result = 0.360454968099581
Inverse Cosine

the result = Acos(the value)

the value: 0.3527 the result = 1.21034135869532
Inverse Tangent

the result = Atan(the value)

the value: 0.3527 the result = 0.339078136684554