Math.js库的使用
可以使用npm或Bower来安装Math.js,直接从Web网站下载,或者通过内容发布网络(CDN)来方法。它可以用于浏览器中,或者用于Node应用程序中。它提供了一组函数来执行操作,例如 add()和multiply()等,支持链化使用。
特征:
- Supports numbers, big numbers, complex numbers, fractions, units, strings, arrays, and matrices.
- Is compatible with JavaScript’s built-in Math library.
- Contains a flexible expression parser.
- Does symbolic computation.
- Comes with a large set of built-in functions and constants.
- Has no dependencies. Runs on any JavaScript engine.
- Can be used as a command line application as well.
- Is easily extensible.
- Open source.
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="math.js | basic usage">
<title>math.js | basic usage</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/3.14.2/math.min.js"></script>
</head>
<body>
<script>
// functions and constants
print(math.round(math.e, 3)); // 2.718
print(math.atan2(3, -3) / math.pi); // 0.75
print(math.log(10000, 10)); // 4
print(math.sqrt(-4)); // 2i
print(math.pow([[-1, 2], [3, 1]], 2)); // [[7, 0], [0, 7]]
print(math.derivative('x^2 + x', 'x')); // 2 * x + 1
// expressions
print(math.eval('12 / (2.3 + 0.7)')); // 4
print(math.eval('12.7 cm to inch')); // 5 inch
print(math.eval('9 / 3 + 2i')); // 3 + 2i
print(math.eval('det([-1, 2; 3, 1])')); // -7
// chaining
var a = math.chain(3)
.add(4)
.multiply(2)
.done();
print(a); // 14
// helper function to output formatted results.
function print(value) {
var precision = 14;
document.write(math.format(value, precision) + '<br>');
}
</script>
</body>
</html>
下载网站
+ website: http://mathjs.org
+ docs: http://mathjs.org/docs
+ examples: http://mathjs.org/examples