Search CTAT

Formula Functions

Applies to CTAT 3.2

Functions can be used in formulas to match student input (see the Edit Student Input Matching dialog), and customize hint and error messages. For more information on how to use functions, see the help section Generalizing an example-tracing tutor with formulas.

algEquiv

Determine whether 2 algebraic expressions are equivalent when fully simplified. Also callable from Jess code: (algEquiv "expression1" "expression2") returns TRUE or FALSE.

Example Usage

algEquiv(input, "4x+9")

returns true if the student input is "9 + 4x" or "9+2x+2x"

More examples of behavior, where == and != indicate that the function returns true or false, respectively:

2x + x - 3 + 5 == 2*x + x + (-3) + 5
2x + x - 3 + 5 == x*2 + 1x + 5 - 3
2x + x - 3 + 5 == 3x + 2
2(x + x - 3) + 5*2 == 2(x + 1x - 3) + 2*5
2(x + x - 3) + 5*2 == (x + 1x - 3)2 + 2*5
2(x + x - 3) + 5*2 == (x + x*1 - 3*1)2 + 2*5
2(x + x - 3) + 5*2 == 5*2 + (x + x + (-3))2

algEquivTerms

Determine whether 2 algebraic expressions are equivalent when simplified to discrete terms. This function will declare 2 expressions equivalent if their differences are limited to order of commutative operands, redundant parentheses and notational variants (2*x and 2x). Also callable from Jess code: (algEquivTerms "expression1" "expression2") returns TRUE or FALSE.

Example Usage

algEquivTerms(input, "4x+9")

returns true if the student input is "9 + 4x" but false if it's "9+2x+2x"

More examples of behavior, where == and != indicate that the function returns true or false, respectively:

2x + x - 3 + 5 == 2*x + x + (-3) + 5
2x + x - 3 + 5 == x*2 + 1x + 5 - 3
2x + x - 3 + 5 != 3x + 2
2(x + x - 3) + 5*2 == 2(x + 1x - 3) + 2*5
2(x + x - 3) + 5*2 == (x + 1x - 3)2 + 2*5
2(x + x - 3) + 5*2 == (x + x*1 - 3*1)2 + 2*5
2(x + x - 3) + 5*2 == 5*2 + (x + x + (-3))2

algEquivTermsSameOrder

Determine whether 2 algebraic expressions are equivalent when simplified to discrete terms. This function will declare 2 expressions equivalent if their differences are limited to order of commutative factor, redundant parentheses and notational variants (2*x and 2x). Unlike algEquivTerms, it will require that addends be in the same order. Also callable from Jess code: (algEquivTermsSameOrder "expression1" "expression2") returns TRUE or FALSE.

Example Usage

algEquivTermsSameOrder(input, "4x+9")

returns true if the student input is "x*4 + 9" but false if it's "9+4x" or "2x+2x+9"

More examples of behavior, where == and != indicate that the function returns true or false, respectively:

2x + x - 3 + 5 == 2*x + x + (-3) + 5
2x + x - 3 + 5 != x*2 + 1x + 5 - 3
2x + x - 3 + 5 != 3x + 2
2(x + x - 3) + 5*2 == 2(x + 1x - 3) + 2*5
2(x + x - 3) + 5*2 == (x + 1x - 3)2 + 2*5
2(x + x - 3) + 5*2 == (x + x*1 - 3*1)2 + 2*5
2(x + x - 3) + 5*2 != 5*2 + (x + x + (-3))2

algEval

Attempt to evaluate an expression given the variable table and BRD-specific settings. Also callable from Jess code: (algEval "expression1") returns a number.

Example Usage

algEval("4x+9")

returns 41 if x=8 in the variable table.

and

Check if two or more conditions are true.

Example Usage

and(greaterThan(input,0),lessThan(input,100))

returns true if the student input is between 0 and 100.

assign

Mechanism for saving values in the current variable table, specific to the interpretation in whose context this function is called.

Example Usage

assign("myvar", "blue")

creates a variable (if none exists) with the name "myvar" and assigns the string value "blue".

assign("mynum", 50)

creates a variable (if none exists) with the name "mynum" and assigns the numeric value 50.

assign("myvar")

removes a variable (if any exists) by the given name "myvar".

chooseRandomly

Choose one of the argument values at random.

chooseRandomly("x","y","z")

concat

Return the result of concatenating all of the arguments as strings.

Example Usage

concat("You entered ",input," pizzas.")

returns "You entered 3 pizzas" if the student input is 3.

constantsConform

A function to tell whether the constants in an expression conform to one or more patterns. This can be used, e.g., to verify that all the numeric values in an equation are either integers or dollar amounts (d.dd).

Example Usage

constantsConform("15 + 8 = x","\d+")
constantsConform("24-x = 7.50","[0-9]+","[0-9][.][0-9][0-9]")

Returns true if the numeric constants in the expression conform to at least one of the patterns.

Parameters:

expr - arithmetic expression with numbers

strPatterns - regular expressions (to be compiled): each number should match one of these

constantsDollar

Check that each number in an expression is either an integer or a dollars-and-cents (d.dd) value. Returns true or false.

deliverDelayedFeedback

Send any delayed feedback messages.

equals

Returns true if all arguments are equal.

Example Usage

equals(0, x, input)

returns false if the variable "x" is set to 0 but the student input is 1.

eval

Simplify an expression. In most cases, it is unnecessary to use the eval function explicitly in formulas. Instead, you can use the expression itself as the formula, e.g., "input/2".

Example Usage

eval(input/2)

returns 5 if the student input is 10.

firstNonNull

Return the left-most non-null argument.

fmtDecimal

Format a decimal number to have the given precision. If precision is zero, omits the decimal point (i.e., rounds to integer). If no precision is specified, precision defaults to 2.

Example Usage

fmtDecimal(input)

returns 5.56 if the student input is 5.559.

equals(fmtDecimal(input, 3),"3.101") 

returns true if the student input, when formatted to 3 decimal points, is equal to 3.101.

fmtDollar

Format a decimal number to have 2 digits to the right of the decimal. If the flags argument contains an "I" then return integer dollar amounts without the trailing ".00". If flags contains a "D" then prefix a dollar sign.

Example Usage

concat("You entered",fmtDollar(input,"D"),".") 

returns "You entered $3.00." if the student input is "3".

fmtNormal

Format a number as a calculator would: as an integer if no fractional part; else with up to 6 digits right of the decimal point, but no trailing zeros.

Example Usage

fmtNormal(input) 

returns 1.333333 if the student input is 1.33333334.

fmtNormal(input, 2)

returns 9.46 if the student input is 09.4559

gcf

Returns the greatest common factor of 2 numbers: e.g., gcf(12,8) returns 4.

getDenominator

Returns the denominator substring in a fraction string "nnn/ddd": e.g., returns "687" from "13/687".

getNumerator

Returns the numerator substring in a fraction string "nnn/ddd": e.g. returns "13" from "13/687".

goToStartState

Send any delayed feedback messages that have accumulated.

greaterThan

Returns true if the first argument is greater than the second, false otherwise. If the arguments cannot both be converted to numbers, then compares lexicographically, ignoring case.

hasValue

Return true if all arguments are non-null.

Example Usage

hasValue(link1.input,link3.input) 

returns true if the student has entered input for link1 and link3.

ifThen

Simulate an if-then-else statement. Returns thenValue if test is true, elseValue otherwise.

ifThen(test, thenValue, elseValue) 

Parameters

test - boolean value to test

thenValue - possible return value

elseValue - alternative return value

Example Usage

ifThen(greaterThan(input,9), input - 10, 0) 

returns 2 if the student input is 12 (i.e., greater than 9), and returns 0 if the student input is 5 (i.e., not greater than 9).

integerInRange

Tell whether an integer is within the range specified by the arguments, inclusive.

integerInRange(n, floor, ceiling) 

Parameters

n - number to test, will convert to integer

floor - left-hand end of the interval; will convert to integer; neg infinity if null

ceiling - right-hand end of the interval

Returns false if n is null; true if floor <= n <= ceiling

Example Usage

integerInRange(input, 0, 9.99) 

returns true if the student input is 0, 9.99, or any number in between.

isAlgValid

Determine whether the argument is a valid algebraic expression.

Example Usage

isAlgValid(input) 

returns true if the student input is 3y+2x+4; returns false if the student input is 3y*+2x+4.

isInteger

Returns true if argument can be interpreted as an integer, false otherwise.

isInteger(value, toStringOk)

If toStringOk is not specified, toStringOk defaults to true.

Parameters

value - number to test

toStringOk - if true, allow the value to be a string (e.g., "25"); if false, value must be numeric

Example Usage

isInteger(input) 

returns true if the student input is 3.0; returns false if the student input is 3.5.

isInteger(x,false) 

returns true if x is 10; returns false if x is "10".

isNumber

Returns true if argument can be interpreted as a number, false otherwise.

isNumber(value, toStringOk)

If toStringOk is not specified, toStringOk defaults to false.

Parameters

value - number to test

toStringOk - if true, allow the value to be a string (e.g., "25"); if false, value must be numeric

Example Usage

isNumber(x) 

returns true if x is an integer or decimal value; returns false otherwise.

isNumber(input,true) 

returns true if the student input is, for example, "6" or 6.5; returns false if the student input is 12x.

last

Returns its last argument, for sequential evaluation. This function can be used when you want to execute some other functions for their side effects but return an arbitrary value. For example, the following formula would assign the String value "fred" a variable first_name and return the student's input:

last(assign("first_name", "fred"), input)

lcm

Return the least common multiple of the 2 arguments: e.g., lcm(6,4) returns 12.

lessThan

Returns true if the first argument is less than the second, false otherwise. If the arguments cannot both be converted to numbers, then compares lexicographically, ignoring case.

matchWithoutPrecision

Return true if input matches number exactly or matches after rounding if input is more precise than number.

matchWithoutPrecision(input, number) 

Parameters

input - numeric value to test; if more precise than number, this value will be rounded before it is compared to number

number - number to match against

Example Usage

matchWithoutPrecision(input, 10) 

returns true if the student input is 9.99.

matchWithPrecision

Return true if input matches number numerically and also has the same precision. For example, if number is .5, input 0.5 matches, but input 0.50 does not match.

matchWithPrecision(input, number) 

memberOf

Return true if the first argument is equal to any of the other arguments, false otherwise.

Example Usage

memberOf(input, x, y, z) 

returns true if the student input is "x", "y", or "z".

mod

Remainder function.

Return n%m, the remainder of n divided by m.

mod(n, m) 
Parameters

n - dividend; if not an integer, will convert to integer by rounding

m - divisor (i.e., modulus); if not an integer, will convert to integer by rounding

Example Usage

mod(input, 4) 

returns 2 as the remainder if the student input is 9.99.

modf

Remainder function. Return n%m, the remainder of n divided by m.

Example Usage

modf(input, 5) 

returns 0.3 if the student input is 10.3.

not

Function to simulate a negation operator. Returns true if test is false, false if test is true.

Example Usage

not(input, 10) 

returns true if the student input is not 10.

or

Returns true if any argument is true, false otherwise.

Example Usage

or(equals(input, 10),equals(input, 5)) 

returns true if the student input is 10 or 5.

plural

Under control of a given count, return the singular or plural form of a word. If the given count is greater than one, make the given word plural by adding an "s". If the given count is greater than one and a suffix to remove and add are specified, make the given word plural by removing a given suffix and appending a different one.

plural(count, word) 
plural(count, word, suffixToStrip, suffixToAdd) 

Returns word if count < 2; else plural form of word

Parameters

count - control value specifying whether or not to pluralize word

word - singular form of word

suffixToStrip - characters to remove from end of word

suffixToAdd - characters to add to end of word

Example Usage

plural(2, "apple") 

returns "apples" as the plural form of "apple".

plural(2, "datum", "um", "a") 

returns "data" as the plural form of "datum".

printf

Format a string with optional arguments inserted, using C-style "%" format specifiers.

Example Usage

printf("You have %d %s.", value1+value2, "books") 

returns "You have 6 books." when value1 is 2 and value2 is 4.

quote

Quote a given string. Returns argument, quoted. Embedded double-quotes are escaped by backslashes.

Example Usage

concat("How many ",quote(input), " dogs?") 

returns "How many "big" dogs?" when the student input is "big".

regExMatch

Tell whether a regular expression accepts a test string.

regExMatch(String regex, String teststr) 
regExMatch(String regex, String teststr, String flags) 

Parameters

regex - regular expression

teststr - test string

flags - case-insensitive letters for flags to Pattern.compile(String, int):

  • 'i': Pattern.CASE_INSENSITIVE;
  • 'm': Pattern.MULTILINE;
  • 'd': Pattern.DOTALL;
  • 'u': Pattern.UNICODE_CASE;
  • 'e': Pattern.CANON_EQ;
  • 'l': Pattern.UNIX_LINES;
  • 't': Pattern.LITERAL;
  • 'c': Pattern.COMMENTS;

Example Usage

regExMatch("x|y", input, "i") 

returns true if the student input is "x", "X", "y", or "Y".

round

Returns the closest integer to the argument.

Example Usage

round(input) 

returns 3 if the student input is 2.59.

runProcess

Experimental class for running an external process.

sum

Add all values and return the result.

Example Usage

sum(input, value1) 

returns 10 if the student input is "4" and value1 is 6.

Math and String classes