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
88 lines (87 loc) • 3.04 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createFraction = void 0;
var _factory = require("../../../utils/factory.js");
var _collection = require("../../../utils/collection.js");
const name = 'fraction';
const dependencies = ['typed', 'Fraction'];
const createFraction = exports.createFraction = /* #__PURE__ */(0, _factory.factory)(name, dependencies, _ref => {
let {
typed,
Fraction
} = _ref;
/**
* 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 | bigint | 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 (x) {
if (!isFinite(x) || isNaN(x)) {
throw new Error(x + ' cannot be represented as a fraction');
}
return new Fraction(x);
},
string: function (x) {
return new Fraction(x);
},
'number, number': function (numerator, denominator) {
return new Fraction(numerator, denominator);
},
null: function (x) {
return new Fraction(0);
},
BigNumber: function (x) {
return new Fraction(x.toString());
},
bigint: function (x) {
return new Fraction(x.toString());
},
Fraction: function (x) {
return x; // fractions are immutable
},
Unit: typed.referToSelf(self => x => {
const clone = x.clone();
clone.value = self(x.value);
return clone;
}),
Object: function (x) {
return new Fraction(x);
},
'Array | Matrix': typed.referToSelf(self => x => (0, _collection.deepMap)(x, self))
});
});
;