Math Operators and Formulas

Important: Starting with version 5.7.1 PCGen has incorporated the JEP (Java Mathematical Expression Parser) library. This was done because the original code which has evolved over time is problematic due to its complexity and lack of documentation. The JEP library has a clearly defined grammar which is available on the web site. JEP supports user defined variables, constants, and functions. A number of common mathematical functions and constants are included. As a fall back, if the JEP parser fails to parse the function then the old code is called. At some point in the future the old code support will be dropped and all formulas must be in JEP syntax.

Using variables within JEP expressions

In most cases variables can be used directly in an expression, however there are some cases when you must use the var() function. This is because some variables can contain characters which are not valid JEP variables. For example the variable CL can be used in a formula without a problem but the variable CL=Fighter cannot be used because of the "=" symbol. In these cases you must use var("CL=Fighter") in the formula for it to parse correctly. The formal description of a JEP variable is "a letter followed by one or more letters and digits" where a letter is defined as: '$', '_', a-z and A-Z.

Examples of variables that may be used alone:

CL, TL, ARMORACCHECK, BardicKnowledgeLevel, TirelessRage.

Examples of variables that must be called with the getvar() function:

var("CL=Fighter"), var("COUNT[FEATS]"), var("COUNT[FEATTYPE=type]").

Consult the DEFINE page for the list of hard coded variables available for use in formulas.

Operator order of processing:

Anything within ()'s are done first, and processing is done left-to-right.

2+(3*5+2)/2

Would become 2+(15+2)/2 (3*5 replaced)

then 2+17/2 (15+2 replaced)

then 19/2 (2+17 replaced)

then 9.5 (result of 19/2).

PCGen truncates (or rounds down to the nearest integer) the results of each formula, so 4/3 will return a 1 and 7/3 will return a 2 etc. If you need to truncate within the formula you can use the min(x,y) or max(x,y) tags.

Operators

2+1

Addition (+).

CL-1

Subtraction (-) - would minus one from the Class Level.

CL/2

Division (/) - would divide the Class Level by two.

CL*3

Multiplication (*) - would multiply the Class Level by three.

((CL+1)+(3*TL)/2)/4

Parenthesis () nesting - the result of three multiplied by Total Level divided by two, plus Class Level + 1 is divided by four.

Updated 5.7.3
Minimums and maximums

The min and max functions support 2 or more arguments, so you can compare 2, 3, 4 or more numbers or variables.

min(a,b,c)

Returns the lowest of 'a', 'b' or 'c'.

max(a,b,c)

Returns the highest of 'a', 'b' or 'c'.

Truncation, rounding up and down.

floor(a)

Returns the highest integer that is less than 'a'.

ceil(a)

Returns the lowest integer that is greater than 'a'.

Boolean operators

Boolean operators are also fully supported. Boolean expressions are evaluated to be either 1 or 0 (true or false respectively).

CL==1

Equal (==). Asks if Class Level is equal to 1.

CL!=1

Not Equal (!=). Asks if Class Level is not equal to 1.

CL>1

Greater than (>). Asks if Class Level is greater than 1.

CL<1

Less than (<). Asks if Class Level is less than 1.

CL>=1

Greater than or Equal (>=). Asks if Class Level is greater than or equal to 1.

CL<=1

Less than or Equal (<=). Asks if Class Level is less than or equal to 1.

(CL>5)&&(TL>5)

Boolean And (&&). Asks if Class Level and Total Level are greater than 5.

(CL>5)||(TL>5)

Boolean Or (||). Asks if Class Level or Total Level is greater than 5.

Boolean If operator

if(x,y,z)

The boolean if operator will return one of two results after evaluating a boolean. The variable x is a boolean result, 0 is false and anything not 0 is true. The variable y is the result returned if x is true, z is the result returned if x is false. Put another way, if x is true (not 0) then the result is y, if x is false (0) then the result is z.

Examples:


if(CL<10,1,2)

Asks if Class Level is less than 10 then returns 1 or else returns 2.

if(CL>=4,10,0)

Asks if Class Level is greater than or equal to 4 then returns 10 or else returns 0.

if((CL>5)||(TL>5),2,-4)

Asks if Class Level or Total Level is greater than 5 then returns 2 or else returns -4.

if(STR,5,0)

Asks if Strength modifier is greater than or less than 0 then returns 5 or else returns 0.

Deprecated operators

The following operators are deprecated as of version 5.7.1. The syntax will be replaced with JEP syntax.

((TL/3).TRUNC)*2

Truncation - would divide TL by 3, truncate (or round down) and then multiply by 2.

Deprecated, use floor(a).

2MIN4

Minimum - would return 2 since it's taking the minimum of the two values (MIN is always between the values).

Deprecated, use min(a,b).

2MAX4

Maximum - would return 4 since it's the max of the two values (MAX is always between the values).

Deprecated, use max(a,b).