美文网首页
优秀的js拓展数学库math.js,解决精度问题

优秀的js拓展数学库math.js,解决精度问题

作者: lifeibest | 来源:发表于2020-06-19 13:39 被阅读0次

    math.js

    今天飞哥推荐一个前端在计算上面必定会用到的类库:mathjs。

    电商系统中,订单、库存中有数字等加减乘除算术,还有做工业计算的,前端怎么算也不对,我们先看看不对的例子。

    JS常见加减乘除算不对的例子

    加法算术:0.1+0.2

    console.log(0.1+0.2);

    结果是:0.30000000000000004

    减法算术:1-0.9

    console.log(1-0.9);

    结果是:0.09999999999999998

    乘法算术 4.10*100

    console.log(4.10*100);

    结果是:409.99999999999994

    除法法算术 6.10/0.1

    console.log(6.10/0.1);

    结果是:60.99999999999999

    超过9007199254740992的大数加法

    console.log(9007199254740992+1);

    结果是9007199254740992

    JavaScript 浮点数运算结果不对,因浮点数的存储问题导致计算结果不对,解决方案是引入数学库math.js,在线上项目中表现很优秀。

    math.js 介绍

    Math.js是一个用于JavaScript和Node.js的扩展数学库。它具有支持符号计算的灵活表达式解析器,大量内置函数和常量,并提供了集成的解决方案来处理不同的数据类型,例如数字,大数,复数,分数,单位和矩阵,强大且易于使用。

    官网:https://mathjs.org/

    Github代码库:https://github.com/josdejong/mathjs

    文档:https://mathjs.org/docs/index.html

    特征

    支持数字,大数,复数,分数,单位,字符串,数组和矩阵。

    与JavaScript的内置Math库兼容,Math用法,一样,门槛低

    包含一个灵活的表达式解析器。

    进行符号计算。

    带有大量内置函数和常量。

    也可以用作命令行应用程序。

    在任何JavaScript引擎上运行。

    很容易扩展。

    开源。

    帮助使用教程:

    1、传统使用,引入math.js

    <!DOCTYPE HTML>

    <html>

    <head>

      <script src="https://unpkg.com/mathjs@7.0.1/dist/math.min.js" type="text/javascript"></script>

    </head>

    <body>

      <script type="text/javascript">

        const ans = math.add(0.1, 0.2)    //  0.30000000000000004

        console.log(math.format(ans, {precision: 14})) // '0.3'

        console.log(math.sqrt(4).toString()) // 2

      </script>

    </body>

    </html>

    npm安装

    npm install mathjs

    2、ES modules

    import { sqrt } from 'mathjs'

    console.log(sqrt(-4).toString()) // 2i

    创建配置

    import { create, all } from 'mathjs'

    // create a mathjs instance with configuration

    const config = {

      epsilon: 1e-12,

      matrix: 'Matrix',

      number: 'number',

      precision: 64,

      predictable: false,

      randomSeed: null

    }

    const math = create(all, config)

    // read the applied configuration

    console.log(math.config())

    // change the configuration

    math.config({

      number: 'BigNumber'

    })

    3、Node.js

    const { sqrt } = require('mathjs')

    console.log(sqrt(-4).toString()) // 2i

    math.js 常用数学功能

    <script type="text/javascript">

    // functions and constants

    console.log('functions and constants')

    print(math.e);  //自然对数  2.718281828459

    print(math.pi);  //π 3.1415926535898

    console.log(Math.random());

    print(math.round(math.e, 3)) //四舍五入,保留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

    console.log()

    // expressions

    console.log('expressions')

    print(math.evaluate('1.2 * (2 + 4.5)')) // 7.8

    print(math.evaluate('12.7 cm to inch')) // 5 inch

    print(math.evaluate('sin(45 deg) ^ 2')) // 0.5

    print(math.evaluate('9 / 3 + 2i')) // 3 + 2i

    print(math.evaluate('det([-1, 2; 3, 1])')) // -7

    console.log()

    // chained operations

    console.log('chained operations')

    const a = math.chain(3)

      .add(4)

      .multiply(2)

      .done()

    print(a) // 14

    console.log()

    // mixed use of different data types in functions

    console.log('mixed use of data types')

    print(math.add(4, [5, 6])) // number + Array, [9, 10]

    print(math.multiply(math.unit('5 mm'), 3)) // Unit * number,  15 mm

    print(math.subtract([2, 3, 4], 5)) // Array - number, [-3, -2, -1]

    print(math.add(math.matrix([2, 3]), [4, 5])) // Matrix + Array, [6, 8]

    console.log()

    /**

    * Helper function to output a value in the console. Value will be formatted.

    * @param {*} value

    */

    function print (value) {

      const precision = 14

      console.log(math.format(value, precision))

    }

    </script>

    math.js 大数功能

    <script type="text/javascript">

    // functions and constants

    console.log('mathjs BigNumber eg')

    print(math.add(math.bignumber(0.1), math.bignumber(0.2))) // 加法 BigNumber, 0.3

    print(math.subtract(math.bignumber(1), math.bignumber(0.9))) // 减法 BigNumber, 0.1

    print(math.multiply(math.bignumber(4.10), math.bignumber(100))) // 乘法 BigNumber, 0.1

    print(math.ceil(math.bignumber(6.10)/ math.bignumber(0.1)));  //除法 向上取整 61

    print(math.floor(math.bignumber(6.10)/ math.bignumber(0.1)));  //除法 向下取整 61

    print(math.round(math.bignumber(6.10)/ math.bignumber(0.1)));  // 除法 四舍五入 61

    /**

    * Helper function to output a value in the console. Value will be formatted.

    * @param {*} value

    */

    function print (value) {

      const precision = 14

      console.log(math.format(value, precision))

    }

    </script>

    原文链接:https://www.ffeeii.com/1992.html

    相关文章

      网友评论

          本文标题:优秀的js拓展数学库math.js,解决精度问题

          本文链接:https://www.haomeiwen.com/subject/yrpvxktx.html