mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-25 15:07:57 +00:00
61 lines
1.7 KiB
Markdown
61 lines
1.7 KiB
Markdown
---
|
|
layout: default
|
|
---
|
|
|
|
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
|
|
|
|
<h1 id="function-evaluate">Function evaluate <a href="#function-evaluate" title="Permalink">#</a></h1>
|
|
|
|
Evaluate an expression.
|
|
|
|
Note the evaluating arbitrary expressions may involve security risks,
|
|
see [https://mathjs.org/docs/expressions/security.html](https://mathjs.org/docs/expressions/security.html) for more information.
|
|
|
|
|
|
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
|
|
|
|
```js
|
|
math.evaluate(expr)
|
|
math.evaluate(expr, scope)
|
|
math.evaluate([expr1, expr2, expr3, ...])
|
|
math.evaluate([expr1, expr2, expr3, ...], scope)
|
|
```
|
|
|
|
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
|
|
|
|
Parameter | Type | Description
|
|
--------- | ---- | -----------
|
|
`expr` | string | string[] | Matrix | The expression to be evaluated
|
|
`scope` | Object | Scope to read/write variables
|
|
|
|
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
|
|
|
|
Type | Description
|
|
---- | -----------
|
|
* | The result of the expression
|
|
|
|
|
|
<h3 id="throws">Throws <a href="#throws" title="Permalink">#</a></h3>
|
|
|
|
Type | Description
|
|
---- | -----------
|
|
Error |
|
|
|
|
<h2 id="examples">Examples <a href="#examples" title="Permalink">#</a></h2>
|
|
|
|
```js
|
|
math.evaluate('(2+3)/4') // 1.25
|
|
math.evaluate('sqrt(3^2 + 4^2)') // 5
|
|
math.evaluate('sqrt(-4)') // 2i
|
|
math.evaluate(['a=3', 'b=4', 'a*b']) // [3, 4, 12]
|
|
|
|
let scope = {a:3, b:4}
|
|
math.evaluate('a * b', scope) // 12
|
|
```
|
|
|
|
|
|
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
|
|
|
|
[parse](parse.html),
|
|
[compile](compile.html)
|