UNPKG

1.71 kBMarkdownView Raw
1<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
2
3# Function compare
4
5Compare two values. Returns 1 when x > y, -1 when x < y, and 0 when x == y.
6
7x and y are considered equal when the relative difference between x and y
8is smaller than the configured epsilon. The function cannot be used to
9compare values smaller than approximately 2.22e-16.
10
11For matrices, the function is evaluated element wise.
12Strings are compared by their numerical value.
13
14
15## Syntax
16
17```js
18math.compare(x, y)
19```
20
21### Parameters
22
23Parameter | Type | Description
24--------- | ---- | -----------
25`x` | number &#124; BigNumber &#124; Fraction &#124; Unit &#124; string &#124; Array &#124; Matrix | First value to compare
26`y` | number &#124; BigNumber &#124; Fraction &#124; Unit &#124; string &#124; Array &#124; Matrix | Second value to compare
27
28### Returns
29
30Type | Description
31---- | -----------
32number &#124; BigNumber &#124; Fraction &#124; Array &#124; 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
38math.compare(6, 1) // returns 1
39math.compare(2, 3) // returns -1
40math.compare(7, 7) // returns 0
41math.compare('10', '2') // returns 1
42math.compare('1000', '1e3') // returns 0
43
44const a = math.unit('5 cm')
45const b = math.unit('40 mm')
46math.compare(a, b) // returns 1
47
48math.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