1 |
|
2 |
|
3 | # Function compare
|
4 |
|
5 | Compare two values. Returns 1 when x > y, -1 when x < y, and 0 when x == y.
|
6 |
|
7 | x and y are considered equal when the relative difference between x and y
|
8 | is smaller than the configured epsilon. The function cannot be used to
|
9 | compare values smaller than approximately 2.22e-16.
|
10 |
|
11 | For matrices, the function is evaluated element wise.
|
12 | Strings are compared by their numerical value.
|
13 |
|
14 |
|
15 | ## Syntax
|
16 |
|
17 | ```js
|
18 | math.compare(x, y)
|
19 | ```
|
20 |
|
21 | ### Parameters
|
22 |
|
23 | Parameter | Type | Description
|
24 | --------- | ---- | -----------
|
25 | `x` | number | BigNumber | Fraction | Unit | string | Array | Matrix | First value to compare
|
26 | `y` | number | BigNumber | Fraction | Unit | string | Array | Matrix | Second value to compare
|
27 |
|
28 | ### Returns
|
29 |
|
30 | Type | Description
|
31 | ---- | -----------
|
32 | number | BigNumber | Fraction | Array | Matrix | Returns the result of the comparison: 1 when x > y, -1 when x < y, and 0 when x == y.
|
33 |
|
34 |
|
35 | ## Examples
|
36 |
|
37 | ```js
|
38 | math.compare(6, 1) // returns 1
|
39 | math.compare(2, 3) // returns -1
|
40 | math.compare(7, 7) // returns 0
|
41 | math.compare('10', '2') // returns 1
|
42 | math.compare('1000', '1e3') // returns 0
|
43 |
|
44 | const a = math.unit('5 cm')
|
45 | const b = math.unit('40 mm')
|
46 | math.compare(a, b) // returns 1
|
47 |
|
48 | math.compare(2, [1, 2, 3]) // returns [1, 0, -1]
|
49 | ```
|
50 |
|
51 |
|
52 | ## See also
|
53 |
|
54 | [equal](equal.md),
|
55 | [unequal](unequal.md),
|
56 | [smaller](smaller.md),
|
57 | [smallerEq](smallerEq.md),
|
58 | [larger](larger.md),
|
59 | [largerEq](largerEq.md),
|
60 | [compareNatural](compareNatural.md),
|
61 | [compareText](compareText.md)
|
62 |
|
\ | No newline at end of file |