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
56 lines • 1.71 kB
JavaScript
import { factory } from '../../utils/factory.js';
import { deepMap } from '../../utils/collection.js';
var name = 'num';
var dependencies = ['typed', 'fraction'];
export var createNum = /* #__PURE__ */factory(name, dependencies, _ref => {
var {
typed,
fraction
} = _ref;
/**
* Get the numerator of a fraction.
* For a fraction `a/b`, the function returns `a`.
*
* The result is always in lowest terms. For example, `num(fraction(8, 6))`
* returns `4n` because 8/6 simplifies to 4/3.
*
* For negative fractions like `-a/b` or `a/-b`, the sign is always
* included in the numerator. Both forms are normalized internally, so
* `num(fraction(-2, 3))` and `num(fraction(2, -3))` both return `-2`.
*
* For matrices, the function is evaluated element wise.
*
* Syntax:
*
* math.num(x)
*
* Examples:
*
* math.num(math.fraction(2, 3)) // returns 2n
* math.num(math.fraction(8, 6)) // returns 4n
* math.num(math.fraction('5/8')) // returns 5n
* math.num(math.fraction(-2, 3)) // returns -2n
* math.num(math.fraction(2, -3)) // returns -2n
* math.num(math.bignumber('0.5')) // returns 1n
*
* See also:
*
* den, fraction
*
* History:
*
* v15.2.0 Created
*
* @param {Fraction | BigNumber | Array | Matrix} x
* A fraction, BigNumber, or array with fractions
* @return {bigint | Array | Matrix} The numerator of x (in lowest terms)
*/
return typed(name, {
Fraction: x => x.s * x.n,
BigNumber: x => {
var f = fraction(x);
return f.s * f.n;
},
'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self))
});
});