mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif
89 lines (88 loc) • 3.13 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createFraction = void 0;
var _factory = require("../../../utils/factory.js");
var _collection = require("../../../utils/collection.js");
var name = 'fraction';
var dependencies = ['typed', 'Fraction'];
var createFraction = exports.createFraction = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
var typed = _ref.typed,
Fraction = _ref.Fraction;
/**
* Create a fraction or convert a value to a fraction.
*
* With one numeric argument, produces the closest rational approximation to the
* input.
* With two arguments, the first is the numerator and the second is the denominator,
* and creates the corresponding fraction. Both numerator and denominator must be
* integers.
* With one object argument, looks for the integer numerator as the value of property
* 'n' and the integer denominator as the value of property 'd'.
* With a matrix argument, creates a matrix of the same shape with entries
* converted into fractions.
*
* Syntax:
* math.fraction(value)
* math.fraction(numerator, denominator)
* math.fraction({n: numerator, d: denominator})
* math.fraction(matrix: Array | Matrix)
*
* Examples:
*
* math.fraction(6.283) // returns Fraction 6283/1000
* math.fraction(1, 3) // returns Fraction 1/3
* math.fraction('2/3') // returns Fraction 2/3
* math.fraction({n: 2, d: 3}) // returns Fraction 2/3
* math.fraction([0.2, 0.25, 1.25]) // returns Array [1/5, 1/4, 5/4]
* math.fraction(4, 5.1) // throws Error: Parameters must be integer
*
* See also:
*
* bignumber, number, string, unit
*
* @param {number | string | Fraction | BigNumber | Unit | Array | Matrix} [args]
* Arguments specifying the value, or numerator and denominator of
* the fraction
* @return {Fraction | Array | Matrix} Returns a fraction
*/
return typed('fraction', {
number: function number(x) {
if (!isFinite(x) || isNaN(x)) {
throw new Error(x + ' cannot be represented as a fraction');
}
return new Fraction(x);
},
string: function string(x) {
return new Fraction(x);
},
'number, number': function numberNumber(numerator, denominator) {
return new Fraction(numerator, denominator);
},
"null": function _null(x) {
return new Fraction(0);
},
BigNumber: function BigNumber(x) {
return new Fraction(x.toString());
},
Fraction: function Fraction(x) {
return x; // fractions are immutable
},
Unit: typed.referToSelf(function (self) {
return function (x) {
var clone = x.clone();
clone.value = self(x.value);
return clone;
};
}),
Object: function Object(x) {
return new Fraction(x);
},
'Array | Matrix': typed.referToSelf(function (self) {
return function (x) {
return (0, _collection.deepMap)(x, self);
};
})
});
});
;