123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual.js';
- import { nearlyEqual } from '../../utils/number.js';
- import { factory } from '../../utils/factory.js';
- import { createMatAlgo03xDSf } from '../../type/matrix/utils/matAlgo03xDSf.js';
- import { createMatAlgo12xSfs } from '../../type/matrix/utils/matAlgo12xSfs.js';
- import { createMatAlgo05xSfSf } from '../../type/matrix/utils/matAlgo05xSfSf.js';
- import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js';
- import { createCompareUnits } from './compareUnits.js';
- var name = 'compare';
- var dependencies = ['typed', 'config', 'matrix', 'equalScalar', 'BigNumber', 'Fraction', 'DenseMatrix'];
- export var createCompare = /* #__PURE__ */factory(name, dependencies, _ref => {
- var {
- typed,
- config,
- equalScalar,
- matrix,
- BigNumber,
- Fraction,
- DenseMatrix
- } = _ref;
- var matAlgo03xDSf = createMatAlgo03xDSf({
- typed
- });
- var matAlgo05xSfSf = createMatAlgo05xSfSf({
- typed,
- equalScalar
- });
- var matAlgo12xSfs = createMatAlgo12xSfs({
- typed,
- DenseMatrix
- });
- var matrixAlgorithmSuite = createMatrixAlgorithmSuite({
- typed,
- matrix
- });
- var compareUnits = createCompareUnits({
- typed
- });
- /**
- * Compare two values. Returns 1 when x > y, -1 when x < y, and 0 when x == y.
- *
- * x and y are considered equal when the relative difference between x and y
- * is smaller than the configured epsilon. The function cannot be used to
- * compare values smaller than approximately 2.22e-16.
- *
- * For matrices, the function is evaluated element wise.
- * Strings are compared by their numerical value.
- *
- * Syntax:
- *
- * math.compare(x, y)
- *
- * Examples:
- *
- * math.compare(6, 1) // returns 1
- * math.compare(2, 3) // returns -1
- * math.compare(7, 7) // returns 0
- * math.compare('10', '2') // returns 1
- * math.compare('1000', '1e3') // returns 0
- *
- * const a = math.unit('5 cm')
- * const b = math.unit('40 mm')
- * math.compare(a, b) // returns 1
- *
- * math.compare(2, [1, 2, 3]) // returns [1, 0, -1]
- *
- * See also:
- *
- * equal, unequal, smaller, smallerEq, larger, largerEq, compareNatural, compareText
- *
- * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} x First value to compare
- * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} y Second value to compare
- * @return {number | BigNumber | Fraction | Array | Matrix} Returns the result of the comparison:
- * 1 when x > y, -1 when x < y, and 0 when x == y.
- */
- return typed(name, createCompareNumber({
- typed,
- config
- }), {
- 'boolean, boolean': function booleanBoolean(x, y) {
- return x === y ? 0 : x > y ? 1 : -1;
- },
- 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
- return bigNearlyEqual(x, y, config.epsilon) ? new BigNumber(0) : new BigNumber(x.cmp(y));
- },
- 'Fraction, Fraction': function FractionFraction(x, y) {
- return new Fraction(x.compare(y));
- },
- 'Complex, Complex': function ComplexComplex() {
- throw new TypeError('No ordering relation is defined for complex numbers');
- }
- }, compareUnits, matrixAlgorithmSuite({
- SS: matAlgo05xSfSf,
- DS: matAlgo03xDSf,
- Ss: matAlgo12xSfs
- }));
- });
- export var createCompareNumber = /* #__PURE__ */factory(name, ['typed', 'config'], _ref2 => {
- var {
- typed,
- config
- } = _ref2;
- return typed(name, {
- 'number, number': function numberNumber(x, y) {
- return nearlyEqual(x, y, config.epsilon) ? 0 : x > y ? 1 : -1;
- }
- });
- });
|