official website
官网:math.js | an extensive math library for JavaScript and Node.js
github:GitHub - josdejong/mathjs: An extensive math library for JavaScript and Node.js
npm address: mathjs-npm
mathjs features:
- Numbers, large numbers, complex numbers, fractions, units, strings, arrays, and matrices are supported.
- Compatible with JavaScript's built-in math library.
- Contains a flexible expression parser.
- Perform symbolic calculations.
- Comes with a large number of built-in functions and constants.
- Can also be used as a command line application.
- Runs on any JavaScript engine.
- Easy to expand.
- open source.
Math.js is available in node.js and the browser.
Install math.js using npm :
npm install mathjs
Or download mathjs via one of the CDNs listed on the download page:
<script src="https://cdn.bootcdn.net/ajax/libs/mathjs/10.6.1/math.min.js"></script>
How to use mathjs
The method of mathjs to obtain the operation value
- valueof()
- done()
- format()
- ...
Use static functions and constants (like JavaScript's Math object)
math.round(math.e, 3); // 2.718
math.add(2, 3); // 5
math.sqrt(-4); // 2i
math.pow([[-1, 2], [3, 1]],2); // [[7, 0], [0, 7]]
math.derivative('x^2 + x', 'x'); // 2 * x + 1
math.atan2(3, -3) / math.pi; // 0.75
mathjs large numbers
If bignumber is not used, the calculation result will have precision problems
math.bignumber('2.3e+500') // BigNumber, 2.3e+500
print(math.add(0.1, 0.2)) // number, 0.30000000000000004
print(math.multiply(10.22, 100)). //1022.0000000000001
print(math.divide(0.3, 0.2)) // number, 1.4999999999999998
The first:
You can use the following function to create BigNumber to solve the precision problem of js calculation
print(math.add(math.bignumber(0.1), math.bignumber(0.2))) // BigNumber, 0.3
print(math.divide(math.bignumber(0.3), math.bignumber(0.2))) // BigNumber, 1.5
The second type:
Most functions can determine the type of output based on the type of input: a number as input will return a number as output, a BigNumber as input will return a BigNumber as output. Functions that cannot determine input and output types (for example math.evaluate
) use default numeric types number
, which can be configured when instantiating math.js. To configure to use BigNumbers instead of numbers by default, configure math.js like this:
const { create, all } = require('..')
// configure the default type of numbers as BigNumbers
const config = {
number: 'BigNumber',
precision: 64,
}
const math = create(all, config)
math.evaluate('0.1 + 0.2') // BigNumber, 0.3
BigNumber has a default precision of 64 bits, which can be configured using the option
Addition, subtraction, multiplication and division
Example using bignumber:
math.add(math.bignumber(0.1), math.bignumber(0.2)) // 加法 BigNumber, 0.3
math.subtract(math.bignumber(1), math.bignumber(0.9)) // 减法 BigNumber, 0.1
math.multiply(math.bignumber(4.10), math.bignumber(100)) // 乘法 BigNumber, 410
math.multiply(math.bignumber(10.22), math.bignumber(100)) //乘法 10.22
math.ceil(math.bignumber(6.10)/ math.bignumber(0.1)); //除法 向上取整 61
math.floor(math.bignumber(6.10)/ math.bignumber(0.1)); //除法 向下取整 61
math.round(math.bignumber(6.10)/ math.bignumber(0.1)); // 除法 四舍五入 61