compare.md (1759B)
1 <!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. --> 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 ### Throws 36 37 Type | Description 38 ---- | ----------- 39 40 41 ## Examples 42 43 ```js 44 math.compare(6, 1) // returns 1 45 math.compare(2, 3) // returns -1 46 math.compare(7, 7) // returns 0 47 math.compare('10', '2') // returns 1 48 math.compare('1000', '1e3') // returns 0 49 50 const a = math.unit('5 cm') 51 const b = math.unit('40 mm') 52 math.compare(a, b) // returns 1 53 54 math.compare(2, [1, 2, 3]) // returns [1, 0, -1] 55 ``` 56 57 58 ## See also 59 60 [equal](equal.md), 61 [unequal](unequal.md), 62 [smaller](smaller.md), 63 [smallerEq](smallerEq.md), 64 [larger](larger.md), 65 [largerEq](largerEq.md), 66 [compareNatural](compareNatural.md), 67 [compareText](compareText.md)