simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

fix.js (3727B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createFix = void 0;
      7 
      8 var _factory = require("../../utils/factory.js");
      9 
     10 var _collection = require("../../utils/collection.js");
     11 
     12 var _algorithm = require("../../type/matrix/utils/algorithm14.js");
     13 
     14 var name = 'fix';
     15 var dependencies = ['typed', 'Complex', 'matrix', 'ceil', 'floor'];
     16 var createFix = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     17   var typed = _ref.typed,
     18       _Complex = _ref.Complex,
     19       matrix = _ref.matrix,
     20       ceil = _ref.ceil,
     21       floor = _ref.floor;
     22   var algorithm14 = (0, _algorithm.createAlgorithm14)({
     23     typed: typed
     24   });
     25   /**
     26    * Round a value towards zero.
     27    * For matrices, the function is evaluated element wise.
     28    *
     29    * Syntax:
     30    *
     31    *    math.fix(x)
     32    *
     33    * Examples:
     34    *
     35    *    math.fix(3.2)                // returns number 3
     36    *    math.fix(3.8)                // returns number 3
     37    *    math.fix(-4.2)               // returns number -4
     38    *    math.fix(-4.7)               // returns number -4
     39    *
     40    *    math.fix(3.12, 1)                // returns number 3.1
     41    *    math.fix(3.18, 1)                // returns number 3.1
     42    *    math.fix(-4.12, 1)               // returns number -4.1
     43    *    math.fix(-4.17, 1)               // returns number -4.1
     44    *
     45    *    const c = math.complex(3.22, -2.78)
     46    *    math.fix(c)                  // returns Complex 3 - 2i
     47    *    math.fix(c, 1)               // returns Complex 3.2 - 2.7i
     48    *
     49    *    math.fix([3.2, 3.8, -4.7])      // returns Array [3, 3, -4]
     50    *    math.fix([3.2, 3.8, -4.7], 1)   // returns Array [3.2, 3.8, -4.7]
     51    *
     52    * See also:
     53    *
     54    *    ceil, floor, round
     55    *
     56    * @param  {number | BigNumber | Fraction | Complex | Array | Matrix} x    Number to be rounded
     57    * @param  {number | BigNumber | Array} [n=0]                             Number of decimals
     58    * @return {number | BigNumber | Fraction | Complex | Array | Matrix}     Rounded value
     59    */
     60 
     61   return typed('fix', {
     62     number: function number(x) {
     63       return x > 0 ? floor(x) : ceil(x);
     64     },
     65     'number, number | BigNumber': function numberNumberBigNumber(x, n) {
     66       return x > 0 ? floor(x, n) : ceil(x, n);
     67     },
     68     Complex: function Complex(x) {
     69       return new _Complex(x.re > 0 ? Math.floor(x.re) : Math.ceil(x.re), x.im > 0 ? Math.floor(x.im) : Math.ceil(x.im));
     70     },
     71     'Complex, number | BigNumber': function ComplexNumberBigNumber(x, n) {
     72       return new _Complex(x.re > 0 ? floor(x.re, n) : ceil(x.re, n), x.im > 0 ? floor(x.im, n) : ceil(x.im, n));
     73     },
     74     BigNumber: function BigNumber(x) {
     75       return x.isNegative() ? ceil(x) : floor(x);
     76     },
     77     'BigNumber, number | BigNumber': function BigNumberNumberBigNumber(x, n) {
     78       return x.isNegative() ? ceil(x, n) : floor(x, n);
     79     },
     80     Fraction: function Fraction(x) {
     81       return x.s < 0 ? x.ceil() : x.floor();
     82     },
     83     'Fraction, number | BigNumber': function FractionNumberBigNumber(x, n) {
     84       return x.s < 0 ? x.ceil(n) : x.floor(n);
     85     },
     86     'Array | Matrix': function ArrayMatrix(x) {
     87       // deep map collection, skip zeros since fix(0) = 0
     88       return (0, _collection.deepMap)(x, this, true);
     89     },
     90     'Array | Matrix, number | BigNumber': function ArrayMatrixNumberBigNumber(x, n) {
     91       var _this = this;
     92 
     93       // deep map collection, skip zeros since fix(0) = 0
     94       return (0, _collection.deepMap)(x, function (i) {
     95         return _this(i, n);
     96       }, true);
     97     },
     98     'number | Complex | BigNumber, Array': function numberComplexBigNumberArray(x, y) {
     99       // use matrix implementation
    100       return algorithm14(matrix(y), x, this, true).valueOf();
    101     }
    102   });
    103 });
    104 exports.createFix = createFix;