UNPKG

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