Math.js compile() Function
Last Updated :
03 Jan, 2023
The Math.js is an extensive library with mathematical functions that work on JavaScript and Node.js. The additional feature of this library is that it provides a flexible expression parser that supports symbolic computation. It is quite powerful and easy to use since it comes with many built-in functions and offers data types such as fractions, complex numbers, matrices, and units.
Math.js Compile function is used for parsing and compiling an expression. The Math.js Compile function returns an object with the compiled expression which is of the function Math.js evaluate([scope]).Â
Syntax:
math.compile(expression)
or
math.compile([expression A, expression B, expression C, ...])
Parameters: This method accepts only one parameter which is mentioned and described below:
- Expression: This parameter is used to specify the expression which has to be compiled.
Return Value: This method returns an object with the compiled expression.
Example 1: This example shows the basic use of the Math.js compile() function.
HTML
<!-- specifying the Mathjs source -->
<script type="text/javascript" src="math.js">
</script>
<script type="text/javascript">
// Defining a constant and calling
// the compile function
const a = math.compile('pow(4, 3)')
// Displaying the output
document.writeln(" Result = "
+ a.evaluate());
</script>
Output:
Result = 64
Example 2: This example explains the above approach.
HTML
<!-- specifying the Mathjs source -->
<script type="text/javascript" src="math.js">
</script>
<script type="text/javascript">
// Defining a scope
let testScope = { a: 30, b: 5 }
// Calling the compile function
const result = math.compile('a/b')
// Displaying the result
document.writeln(" Result = "
+ result.evaluate(testScope));
</script>
Output:
Result = 6
Example 3: This example shows the basic use of the Math.js compile() function.
HTML
<!-- specifying the Mathjs source -->
<script type="text/javascript" src="math.js">
</script>
<script type="text/javascript">
// Defining a node and calling
// the compile function
const testNode = math.compile(
['a = 12', 'b = 2', 'a / b'])
// Displaying the result
document.writeln(" Result = "
+ testNode[2].evaluate());
</script>
Output:
Result = 6
Note: This will not work in normal JavaScript because it requires the Math.js library to be installed.
Similar Reads
Less.js Math ceil() Function Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is an extension to normal CSS which basically enhances the abilities of normal CSS and gives superpowers to it. LESS.js provides the built-in Math function that
3 min read
JavaScript Math.ceil( ) function The JavaScript Math.ceil() function rounds a given number up to the nearest integer. It always rounds towards positive infinity, meaning it increases the number to the next whole number if it's not already an integer. This function is useful for rounding up values in calculations.SyntaxMath.ceil(val
2 min read
Less.js Math Functions In this article, we will see the various Math functions that are provided by Less.js to perform various mathematical functions as per the user's requirements within CSS code only. Less.js (Leaner Style Sheets) is an extension to normal CSS which basically enhances the abilities of normal CSS and giv
6 min read
Less.js Misc convert() Function Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is chosen because CSS is a dynamic style sheet language. LESS is flexible, thus it functions with a variety of browsers. Web browsers can only use CSS that has
4 min read
How to Create Custom Functions in math.js? Custom Functions in Math.js are nothing but user-defined functions that extend the capabilities of the library. They allow you to implement specific logic or calculations that are not available in the standard Math.js library. In this article, we will learn to create custom functions in Math.js to p
2 min read
JavaScript Function Complete Reference A JavaScript function is a set of statements that takes inputs, performs specific computations, and produces outputs. Essentially, a function performs tasks or computations and then returns the result to the user.Syntax:function functionName(Parameter1, Parameter2, ..) { // Function body}Example: Be
3 min read