math.eval

From Inspired-Lua Wiki
Revision as of 21:01, 19 April 2016 by Adriweb (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

math.eval is a math library extension.

This has been introduced in TI-Nspire OS 3.0 (Changes).


The function calls the math server with the specified arguments and returns the a numerical result if possible.

Beginning with platform.apiLevel = '2.0' (OS 3.2), 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 overriden 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.

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:
Boolean logic: r,e = math.eval('1 and 2') returns "Argument must be a Boolean expression or integer" error
Convert to base 10 r,e = math.eval("0@>Base10") returns "Domain Error"
math.evalStr works fine in such cases.


Syntax

math.eval(expression)
evaluates "expression" (can contain TI-Basic arguments) and returns a result as a number if possible.

Expression can contain everything a TI-Basic function can contain. You can seperate statements using ":".
It can also contain calls to user defined functions in the document, or functions in global libraries.


Example

math.eval("sqrt(16)") -- returns 4


See also