Difference between revisions of "math.eval"
Jimbauwens (talk | contribs) |
m |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | <span style="font-weight: bold;">math.eval</span> is a math library extension. | + | <span style="font-weight: bold;">math.eval</span> is a math library extension. <br /> |
+ | |||
+ | {{Since|3.0}} | ||
− | |||
The function calls the math server with the specified arguments and returns the a numerical result if possible.<br> | The function calls the math server with the specified arguments and returns the a numerical result if possible.<br> | ||
− | == | + | 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. <br /> |
+ | If the math server evaluates the expression successfully, it returns the results as a fundamental Lua data type. <br /> | ||
+ | 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.)<br /> | ||
+ | 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." <br /> | ||
+ | |||
+ | '''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. <br /> | ||
+ | |||
+ | <source lang="lua">local mx = tostring(x):gsub("e", string.uchar(0xF000)) | ||
+ | local expr = "f1(" .. mx .. ")" | ||
+ | return math.eval(expr)</source> | ||
+ | |||
+ | '''Note''' : Because ''math.eval'' always does calculations in approximate mode, things like Boolean logic and some conversions will throw an error: <br /> | ||
+ | Boolean logic: r,e = math.eval('1 and 2') returns "Argument must be a Boolean expression or integer" error <br /> | ||
+ | Convert to base 10 r,e = math.eval("0@>Base10") returns "Domain Error" <br /> | ||
+ | [[math.evalStr]] works fine in such cases. | ||
− | |||
− | + | == Syntax == | |
+ | math.'''eval(expression)'''<br> evaluates "expression" (can contain TI-Basic arguments) and returns a result as a number if possible.<br> | ||
+ | Expression can contain everything a TI-Basic function can contain. You can seperate statements using ":". <br /> | ||
It can also contain calls to user defined functions in the document, or functions in global libraries. | It can also contain calls to user defined functions in the document, or functions in global libraries. | ||
+ | |||
== Example == | == Example == | ||
+ | <source lang="lua">math.eval("sqrt(16)") -- returns 4</source> | ||
− | |||
− | + | == See also == | |
+ | [[math.evalStr]] | ||
− | + | ||
+ | [[Category:Extended_Standard_Library]] |
Latest revision as of 21:01, 19 April 2016
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