0
点赞
收藏
分享

微信扫一扫

数学公式库 mathjs 安装使用


Math.js 是个JavaScript 和 Node.js 的扩展数学库。它包括了灵活的表达式解析器,提供数字,大数值,复杂数值,单位,矩阵等等集成的解决方案。Math.js 很强大又易于使用。

下载引用

官网:https://mathjs.org/index.html

安装:npm install mathjs

CDN:https://cdnjs.cloudflare.com/ajax/libs/mathjs/14.2.1/math.min.js

math.js使用方式:

OperatorNodeMap = {
  xor: 'xor'
  and: 'and'
  or: 'or'
  bitOr: '|'
  bitXor: '^|'
  bitAnd: '&'
  equal: '=='
  unequal: '!='
  smaller: '<'
  larger: '>'
  smallerEq: '<='
  largerEq: '>='
  leftShift: '<<'
  rightArithShift: '>>'
  rightLogShift: '>>>'
  to: 'to'
  add: '+'
  subtract: '-'
  multiply: '*'
  divide: '/'
  dotMultiply: '.*'
  dotDivide: './'
  mod: 'mod'
  unaryPlus: '+'
  unaryMinus: '-'
  bitNot: '~'
  not: 'not'
  pow: '^'
  dotPow: '.^'
  factorial: '!'
}

加法:math.add(2, 3)  // 5

减法:math.subtract(2, 3)  // -1

乘法:math.multiply(2, 3) // 6

除法:math.divide(2, 3)  // 0.6666666666666666

链式操作:math.chain(3).add(4).multiply(2).done();  // 14

计算精度丢失问题:

math.add(0.1, 0.2)  // 0.30000000000000004

解决方法:math.add(math.bignumber(0.1), math.bignumber(0.2))  // 0.3

四舍五入保留 N 位小数:math.round(5.3692, 2)  // 5.37

其它精度算术运算的JavaScript库

decimal.js bignumber.js big.js Calculator.js

举报

相关推荐

0 条评论