cmp

cmp(x,y)

Return -1, 0, or 1 depending on whether x is less than, equal to, or greater than y, respectively. Uses the total order implemented by isless. For floating-point numbers, uses < but throws an error for unordered arguments.

Examples

In the Julia programming language, the function cmp(x, y)

Return -1, 0, or 1 depending on whether x is less than, equal to, or greater than y, respectively. It uses the total order implemented by isless. For floating-point numbers, it uses < but throws an error for unordered arguments.

julia> cmp(5, 10)
-1

julia> cmp(10, 10)
0

julia> cmp("apple", "banana")
-1

julia> cmp(3.14, 2.71)
1

Here are some common examples of its use:

  1. Comparing integers:

    julia> cmp(5, 10)
    -1

    In this example, cmp(5, 10) returns -1 because 5 is less than 10.

  2. Comparing strings:

    julia> cmp("apple", "banana")
    -1

    It returns -1 because "apple" comes before "banana" in lexicographic order.

  3. Comparing floating-point numbers:

    julia> cmp(3.14, 2.71)
    1

    The function returns 1 because 3.14 is greater than 2.71.

Note: The function throws an error if the arguments are unordered, such as when trying to compare NaN (not-a-number) values.

See Also

cmp, float, get_bigfloat_precision, get_rounding, get_zero_subnormals, isapprox, maxintfloat, mod2pi, nextfloat, precision, prevfloat, rationalize, round, set_bigfloat_precision, set_rounding, set_zero_subnormals, significand, with_bigfloat_precision, with_rounding,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: