UNPKG

1.82 kBJavaScriptView Raw
1import { factory } from '../../utils/factory';
2import { deepMap } from '../../utils/collection';
3var name = 'fix';
4var dependencies = ['typed', 'Complex', 'ceil', 'floor'];
5export var createFix =
6/* #__PURE__ */
7factory(name, dependencies, function (_ref) {
8 var typed = _ref.typed,
9 _Complex = _ref.Complex,
10 ceil = _ref.ceil,
11 floor = _ref.floor;
12
13 /**
14 * Round a value towards zero.
15 * For matrices, the function is evaluated element wise.
16 *
17 * Syntax:
18 *
19 * math.fix(x)
20 *
21 * Examples:
22 *
23 * math.fix(3.2) // returns number 3
24 * math.fix(3.8) // returns number 3
25 * math.fix(-4.2) // returns number -4
26 * math.fix(-4.7) // returns number -4
27 *
28 * const c = math.complex(3.2, -2.7)
29 * math.fix(c) // returns Complex 3 - 2i
30 *
31 * math.fix([3.2, 3.8, -4.7]) // returns Array [3, 3, -4]
32 *
33 * See also:
34 *
35 * ceil, floor, round
36 *
37 * @param {number | BigNumber | Fraction | Complex | Array | Matrix} x Number to be rounded
38 * @return {number | BigNumber | Fraction | Complex | Array | Matrix} Rounded value
39 */
40 var fix = typed('fix', {
41 number: function number(x) {
42 return x > 0 ? floor(x) : ceil(x);
43 },
44 Complex: function Complex(x) {
45 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));
46 },
47 BigNumber: function BigNumber(x) {
48 return x.isNegative() ? ceil(x) : floor(x);
49 },
50 Fraction: function Fraction(x) {
51 return x.s < 0 ? x.ceil() : x.floor();
52 },
53 'Array | Matrix': function ArrayMatrix(x) {
54 // deep map collection, skip zeros since fix(0) = 0
55 return deepMap(x, fix, true);
56 }
57 });
58 return fix;
59});
\No newline at end of file