compare.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { nearlyEqual as bigNearlyEqual } from '../../utils/bignumber/nearlyEqual.js';
  2. import { nearlyEqual } from '../../utils/number.js';
  3. import { factory } from '../../utils/factory.js';
  4. import { createMatAlgo03xDSf } from '../../type/matrix/utils/matAlgo03xDSf.js';
  5. import { createMatAlgo12xSfs } from '../../type/matrix/utils/matAlgo12xSfs.js';
  6. import { createMatAlgo05xSfSf } from '../../type/matrix/utils/matAlgo05xSfSf.js';
  7. import { createMatrixAlgorithmSuite } from '../../type/matrix/utils/matrixAlgorithmSuite.js';
  8. import { createCompareUnits } from './compareUnits.js';
  9. var name = 'compare';
  10. var dependencies = ['typed', 'config', 'matrix', 'equalScalar', 'BigNumber', 'Fraction', 'DenseMatrix'];
  11. export var createCompare = /* #__PURE__ */factory(name, dependencies, _ref => {
  12. var {
  13. typed,
  14. config,
  15. equalScalar,
  16. matrix,
  17. BigNumber,
  18. Fraction,
  19. DenseMatrix
  20. } = _ref;
  21. var matAlgo03xDSf = createMatAlgo03xDSf({
  22. typed
  23. });
  24. var matAlgo05xSfSf = createMatAlgo05xSfSf({
  25. typed,
  26. equalScalar
  27. });
  28. var matAlgo12xSfs = createMatAlgo12xSfs({
  29. typed,
  30. DenseMatrix
  31. });
  32. var matrixAlgorithmSuite = createMatrixAlgorithmSuite({
  33. typed,
  34. matrix
  35. });
  36. var compareUnits = createCompareUnits({
  37. typed
  38. });
  39. /**
  40. * Compare two values. Returns 1 when x > y, -1 when x < y, and 0 when x == y.
  41. *
  42. * x and y are considered equal when the relative difference between x and y
  43. * is smaller than the configured epsilon. The function cannot be used to
  44. * compare values smaller than approximately 2.22e-16.
  45. *
  46. * For matrices, the function is evaluated element wise.
  47. * Strings are compared by their numerical value.
  48. *
  49. * Syntax:
  50. *
  51. * math.compare(x, y)
  52. *
  53. * Examples:
  54. *
  55. * math.compare(6, 1) // returns 1
  56. * math.compare(2, 3) // returns -1
  57. * math.compare(7, 7) // returns 0
  58. * math.compare('10', '2') // returns 1
  59. * math.compare('1000', '1e3') // returns 0
  60. *
  61. * const a = math.unit('5 cm')
  62. * const b = math.unit('40 mm')
  63. * math.compare(a, b) // returns 1
  64. *
  65. * math.compare(2, [1, 2, 3]) // returns [1, 0, -1]
  66. *
  67. * See also:
  68. *
  69. * equal, unequal, smaller, smallerEq, larger, largerEq, compareNatural, compareText
  70. *
  71. * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} x First value to compare
  72. * @param {number | BigNumber | Fraction | Unit | string | Array | Matrix} y Second value to compare
  73. * @return {number | BigNumber | Fraction | Array | Matrix} Returns the result of the comparison:
  74. * 1 when x > y, -1 when x < y, and 0 when x == y.
  75. */
  76. return typed(name, createCompareNumber({
  77. typed,
  78. config
  79. }), {
  80. 'boolean, boolean': function booleanBoolean(x, y) {
  81. return x === y ? 0 : x > y ? 1 : -1;
  82. },
  83. 'BigNumber, BigNumber': function BigNumberBigNumber(x, y) {
  84. return bigNearlyEqual(x, y, config.epsilon) ? new BigNumber(0) : new BigNumber(x.cmp(y));
  85. },
  86. 'Fraction, Fraction': function FractionFraction(x, y) {
  87. return new Fraction(x.compare(y));
  88. },
  89. 'Complex, Complex': function ComplexComplex() {
  90. throw new TypeError('No ordering relation is defined for complex numbers');
  91. }
  92. }, compareUnits, matrixAlgorithmSuite({
  93. SS: matAlgo05xSfSf,
  94. DS: matAlgo03xDSf,
  95. Ss: matAlgo12xSfs
  96. }));
  97. });
  98. export var createCompareNumber = /* #__PURE__ */factory(name, ['typed', 'config'], _ref2 => {
  99. var {
  100. typed,
  101. config
  102. } = _ref2;
  103. return typed(name, {
  104. 'number, number': function numberNumber(x, y) {
  105. return nearlyEqual(x, y, config.epsilon) ? 0 : x > y ? 1 : -1;
  106. }
  107. });
  108. });