UNPKG

2.12 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createFraction = void 0;
7
8var _factory = require("../../../utils/factory");
9
10var _collection = require("../../../utils/collection");
11
12var name = 'fraction';
13var dependencies = ['typed', 'Fraction'];
14var createFraction = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
15 var typed = _ref.typed,
16 Fraction = _ref.Fraction;
17
18 /**
19 * Create a fraction convert a value to a fraction.
20 *
21 * Syntax:
22 * math.fraction(numerator, denominator)
23 * math.fraction({n: numerator, d: denominator})
24 * math.fraction(matrix: Array | Matrix) Turn all matrix entries
25 * into fractions
26 *
27 * Examples:
28 *
29 * math.fraction(1, 3)
30 * math.fraction('2/3')
31 * math.fraction({n: 2, d: 3})
32 * math.fraction([0.2, 0.25, 1.25])
33 *
34 * See also:
35 *
36 * bignumber, number, string, unit
37 *
38 * @param {number | string | Fraction | BigNumber | Array | Matrix} [args]
39 * Arguments specifying the numerator and denominator of
40 * the fraction
41 * @return {Fraction | Array | Matrix} Returns a fraction
42 */
43 return typed('fraction', {
44 number: function number(x) {
45 if (!isFinite(x) || isNaN(x)) {
46 throw new Error(x + ' cannot be represented as a fraction');
47 }
48
49 return new Fraction(x);
50 },
51 string: function string(x) {
52 return new Fraction(x);
53 },
54 'number, number': function numberNumber(numerator, denominator) {
55 return new Fraction(numerator, denominator);
56 },
57 "null": function _null(x) {
58 return new Fraction(0);
59 },
60 BigNumber: function BigNumber(x) {
61 return new Fraction(x.toString());
62 },
63 Fraction: function Fraction(x) {
64 return x; // fractions are immutable
65 },
66 Object: function Object(x) {
67 return new Fraction(x);
68 },
69 'Array | Matrix': function ArrayMatrix(x) {
70 return (0, _collection.deepMap)(x, this);
71 }
72 });
73});
74exports.createFraction = createFraction;
\No newline at end of file