How to use the mathjs.format function in mathjs

To help you get started, we’ve selected a few mathjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lin-xin / calculator / static / script / index.js View on Github external
case '4': // 10 的 x 次幂
                res.innerHTML = result = math.format(math.pow(10, result), {
                    precision: 16
                });
                _this.register.ope = type;
                _this.isEqual = true;
                break;
            case '5': // 1/x
                res.innerHTML = result = math.format(math.divide(1, result), {
                    precision: 16
                });
                _this.register.ope = type;
                _this.isEqual = true;
                break;
            case '6': // x 开平方根
                res.innerHTML = result = math.format(math.sqrt(result), {
                    precision: 16
                });
                _this.register.ope = type;
                _this.isEqual = true;
                break;
            case '7': // x 开立方根
                res.innerHTML = result = math.format(math.cbrt(result), {
                    precision: 16
                });
                _this.register.ope = type;
                _this.isEqual = true;
                break;
            case '8': // x 的开 y 次方根
                _this.events = event;
                if (!event.classList.contains('active')) {
                    if (_this.register.ope === '8') {
github lin-xin / calculator / static / script / index.js View on Github external
case '18': // 底数为 e 的对数
                res.innerHTML = result = math.format(math.log(result), {
                    precision: 16
                });
                _this.register.ope = type;
                _this.isEqual = true;
                break;
            case '19': // 底数为 10 的对数
                res.innerHTML = result = math.format(math.log10(result), {
                    precision: 16
                });
                _this.register.ope = type;
                _this.isEqual = true;
                break;
            case '20': // e 的指数
                res.innerHTML = result = math.format(math.exp(result), {
                    precision: 16
                });
                _this.register.ope = type;
                _this.isEqual = true;
                break;
            default:
                break;
        }
    },
github lin-xin / calculator / static / script / index.js View on Github external
case '13': // sinh 双曲正弦
                res.innerHTML = result = math.format(math.sinh(result), {
                    precision: 16
                });
                _this.register.ope = type;
                _this.isEqual = true;
                break;
            case '14': // cosh 双曲余弦
                res.innerHTML = result = math.format(math.cosh(result), {
                    precision: 16
                });
                _this.register.ope = type;
                _this.isEqual = true;
                break;
            case '15': // tanh 双曲正切
                res.innerHTML = result = math.format(math.tanh(result), {
                    precision: 16
                });
                _this.register.ope = type;
                _this.isEqual = true;
                break;
            case '16': // 圆周率
                res.innerHTML = result = Math.PI;
                _this.register.ope = type;
                _this.isEqual = true;
                break;
            case '17': // x 的阶乘
                if (result.indexOf('.') > -1 || result.indexOf('-') > -1) {
                    _this.reset();
                    res.innerHTML = 'error';
                } else {
                    res.innerHTML = result = math.factorial(result);
github uccser / cs-field-guide / csfieldguide / static / interactives / algorithm-timer / js / algorithm-timer.js View on Github external
steps = Mathjs.bignumber(Math.pow(2, n));
  } else if (complexity == 'factorial') {
    n = Mathjs.bignumber(n);
    steps = Mathjs.factorial(n);
  }

  var denominator = Mathjs.multiply(speed, processors);
  var timeTaken = Mathjs.divide(steps, denominator);

  var timeScale = Mathjs.bignumber(TIME_SCALERS[timeUnits])
  timeTaken = Mathjs.multiply(timeTaken, timeScale);

  if (resultForm == 'scientific') {
    timeTaken = Mathjs.format(Mathjs.bignumber(timeTaken), {notation: 'exponential', precision: 3});
  } else {
    timeTaken = Mathjs.format(Mathjs.bignumber(timeTaken), {notation: 'fixed', precision: 2});
      // If there are no significant decimal places don't show any decimal places (non scientific mode only)
    if (timeTaken % 1 == 0) {
      timeTaken = Mathjs.format(Mathjs.bignumber(timeTaken), {notation: 'fixed', precision: 0});
    }
  }

  return timeTaken;
}
github josdejong / mathjs / examples / fractions.js View on Github external
function printRatio (value) {
  console.log(math.format(value, { fraction: 'ratio' }))
}
github josdejong / mathjs / examples / chaining.js View on Github external
function print (value) {
  const precision = 14
  console.log(math.format(value, precision))
}
github josdejong / mathjs / examples / complex_numbers.js View on Github external
function print (value) {
  const precision = 14
  console.log(math.format(value, precision))
}
github josdejong / mathjs / examples / fractions.js View on Github external
function print (value) {
  console.log(math.format(value, { fraction: 'decimal' }))
}
github Qwant / Instant-Answers / app / src / modules / calculator / calculator.js View on Github external
return new Promise(function (resolve, reject) {
            if(!isNaN(values[0])) {
                reject("Don't allow function identity", {module : "calculator"});
            } else {
                values[0] = values[0].toLowerCase();
                values[0] = values[0].replace("×","*");
                values[0] = values[0].replace("x","*");
                values[0] = values[0].replace("÷","/");
                values[0] = values[0].replace("¹", "^1");
                values[0] = values[0].replace("²", "^2");
                values[0] = values[0].replace("³", "^3");
                try {
                    var response = math.eval(values[0]);
                    response = math.format(response, {precision: 14});
                    if(response === 'function') {
                        reject("The query is poorly formulated", {module : "calculator"});
                    }
                    if (response['signatures']) {
                        resolve(0);
                    } else {
                        if (response === Infinity) {
                            throw 500;
                        } else {
                            resolve(response.toString());
                        }
                    }
                } catch (e) {
                    reject("Your formula isn't valid", {module : "calculator"});
                }
            }
github antoniodeluca / dn2a / assets / synapse.js View on Github external
this._outgoingConnection = null;

        this._previousIncomingConnections = null;

        this._previousOutgoingConnections = null;

        this._previousWeight = m.bignumber(0);

        this._previousWeightChange = m.bignumber(0);

        this._weight = m.subtract(
            m.bignumber(1),
            m.multiply(
                m.bignumber(
                    m.format(
                        m.random(0, 1),
                        {
                            notation: "fixed",
                            precision: 15
                        }
                    )
                ),
                m.bignumber(2)
            )
        );

        this._weightChange = m.bignumber(0);
    };

mathjs

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif

Apache-2.0
Latest version published 1 month ago

Package Health Score

91 / 100
Full package analysis