You are here: Lua Scripting Resources eGuide > Lua Scripting API Reference Guide > Math Library Extension > eval

12.1 eval

math.eval(math_expression) -- platform.apiLevel = '2.0' 
math.eval(math_expression [,exact]) -- platform.apiLevel = '1.0'

This function sends an expression or command to the TI-Nspire™ math server for evaluation. The input expression must be a string that the TI-Nspire™ math server can interpret and
evaluate.

The second parameter, exact, (platform.apiLevel = '1.0' only) is meaningful only with the Computer Algebra System. If true, it instructs the math server to calculate and return exact numerical results when it can. The default value of exact is false, in which case the math server attempts to calculate an approximate result.

Beginning with platform.apiLevel = '2.0', the evaluation is performed using the current document settings, except that all evaluations are performed at full precision in approximate mode. The current document settings can be overridden by math.setEvalSettings.

If the math server evaluates the expression successfully, it returns the results as a fundamental Lua data type. If the math server cannot evaluate the expression because of a syntax, simplification, or semantic error, eval returns two results: nil and an error number meaningful to the math server. (The error numbers are documented in the TI-Nspire™ Reference Guide - Error Codes and Messages for math.eval.) If the math server calculates a symbolic result, it cannot be represented as a fundamental Lua type, so eval returns nil and the string “incompatible data type.”

Example

To evaluate f1 for a given value in x, the parameter x must be converted to a string, and then any embedded “e” must be replaced with Unicode character U+F000.

Listing 12.1:
Converting a Lua Number to a String to be Used in math.eval() (E Notation)

local mx = tostring(x):gsub("e", string.uchar(0xF000)) 
local expr = "f1(" .. mx .. ")"
return math.eval(expr)

Note

Because math.eval always does calculations in approximate mode, things like Boolean logic and some conversions will throw an error:

r,e = math.eval('1 and 2') returns “Argument must be a Boolean expression or integer” error

r,e = math.eval(”0@>Base10”) returns “Domain Error”

math.evalStr works fine in such cases.

Warning

math.eval is not available during script initialization.

Introduced in platform.apiLevel = '1.0'

Extended in platform.apiLevel = ‘2.0’