simple-squiggle

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

floor.js (4369B)


      1 import Decimal from 'decimal.js';
      2 import { factory } from '../../utils/factory.js';
      3 import { deepMap } from '../../utils/collection.js';
      4 import { nearlyEqual } from '../../utils/number.js';
      5 import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual.js';
      6 import { createAlgorithm11 } from '../../type/matrix/utils/algorithm11.js';
      7 import { createAlgorithm14 } from '../../type/matrix/utils/algorithm14.js';
      8 var name = 'floor';
      9 var dependencies = ['typed', 'config', 'round', 'matrix', 'equalScalar'];
     10 export var createFloor = /* #__PURE__ */factory(name, dependencies, _ref => {
     11   var {
     12     typed,
     13     config,
     14     round,
     15     matrix,
     16     equalScalar
     17   } = _ref;
     18   var algorithm11 = createAlgorithm11({
     19     typed,
     20     equalScalar
     21   });
     22   var algorithm14 = createAlgorithm14({
     23     typed
     24   });
     25   /**
     26    * Round a value towards minus infinity.
     27    * For matrices, the function is evaluated element wise.
     28    *
     29    * Syntax:
     30    *
     31    *    math.floor(x)
     32    *    math.floor(x, n)
     33    *
     34    * Examples:
     35    *
     36    *    math.floor(3.2)              // returns number 3
     37    *    math.floor(3.8)              // returns number 3
     38    *    math.floor(-4.2)             // returns number -5
     39    *    math.floor(-4.7)             // returns number -5
     40    *
     41    *    math.floor(3.212, 2)          // returns number 3.21
     42    *    math.floor(3.288, 2)          // returns number 3.28
     43    *    math.floor(-4.212, 2)         // returns number -4.22
     44    *    math.floor(-4.782, 2)         // returns number -4.79
     45    *
     46    *    const c = math.complex(3.24, -2.71)
     47    *    math.floor(c)                 // returns Complex 3 - 3i
     48    *    math.floor(c, 1)              // returns Complex 3.2 - 2.8i
     49    *
     50    *    math.floor([3.2, 3.8, -4.7])       // returns Array [3, 3, -5]
     51    *    math.floor([3.21, 3.82, -4.71], 1)  // returns Array [3.2, 3.8, -4.8]
     52    *
     53    * See also:
     54    *
     55    *    ceil, fix, round
     56    *
     57    * @param  {number | BigNumber | Fraction | Complex | Array | Matrix} x  Number to be rounded
     58    * @param  {number | BigNumber | Array} [n=0]                            Number of decimals
     59    * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
     60    */
     61 
     62   return typed('floor', {
     63     number: function number(x) {
     64       if (nearlyEqual(x, round(x), config.epsilon)) {
     65         return round(x);
     66       } else {
     67         return Math.floor(x);
     68       }
     69     },
     70     'number, number': function numberNumber(x, n) {
     71       if (nearlyEqual(x, round(x, n), config.epsilon)) {
     72         return round(x, n);
     73       } else {
     74         var [number, exponent] = "".concat(x, "e").split('e');
     75         var result = Math.floor(Number("".concat(number, "e").concat(Number(exponent) + n)));
     76         [number, exponent] = "".concat(result, "e").split('e');
     77         return Number("".concat(number, "e").concat(Number(exponent) - n));
     78       }
     79     },
     80     Complex: function Complex(x) {
     81       return x.floor();
     82     },
     83     'Complex, number': function ComplexNumber(x, n) {
     84       return x.floor(n);
     85     },
     86     BigNumber: function BigNumber(x) {
     87       if (bigNearlyEqual(x, round(x), config.epsilon)) {
     88         return round(x);
     89       } else {
     90         return x.floor();
     91       }
     92     },
     93     'BigNumber, BigNumber': function BigNumberBigNumber(x, n) {
     94       if (bigNearlyEqual(x, round(x, n), config.epsilon)) {
     95         return round(x, n);
     96       } else {
     97         return x.toDecimalPlaces(n.toNumber(), Decimal.ROUND_FLOOR);
     98       }
     99     },
    100     Fraction: function Fraction(x) {
    101       return x.floor();
    102     },
    103     'Fraction, number': function FractionNumber(x, n) {
    104       return x.floor(n);
    105     },
    106     'Array | Matrix': function ArrayMatrix(x) {
    107       // deep map collection, skip zeros since floor(0) = 0
    108       return deepMap(x, this, true);
    109     },
    110     'Array | Matrix, number': function ArrayMatrixNumber(x, n) {
    111       // deep map collection, skip zeros since ceil(0) = 0
    112       return deepMap(x, i => this(i, n), true);
    113     },
    114     'SparseMatrix, number | BigNumber': function SparseMatrixNumberBigNumber(x, y) {
    115       return algorithm11(x, y, this, false);
    116     },
    117     'DenseMatrix, number | BigNumber': function DenseMatrixNumberBigNumber(x, y) {
    118       return algorithm14(x, y, this, false);
    119     },
    120     'number | Complex | BigNumber, Array': function numberComplexBigNumberArray(x, y) {
    121       // use matrix implementation
    122       return algorithm14(matrix(y), x, this, true).valueOf();
    123     }
    124   });
    125 });