COMPARE_DECFLOAT

The COMPARE_DECFLOAT function returns an ordering for DECFLOAT values.

Read syntax diagramSkip visual syntax diagram
>>-COMPARE_DECFLOAT--(--expression-1--,--expression-2--)-------><

The COMPARE_DECFLOAT function returns a small integer value that indicates how expression-1 compares with expression-2.

expression-1
An expression that returns a value of any built-in numeric, character-string, or graphic-string data type. If the argument is not DECFLOAT(34), it is logically converted to DECFLOAT(34) for processing.
expression-2
An expression that returns a value of any built-in numeric, character-string, or graphic-string data type. If the argument is not DECFLOAT(34), it is logically converted to DECFLOAT(34) for processing.

The first argument is compared with the second argument and the result is returned according to the following rules.

  • If both operands are finite, the comparison is algebraic and follows the Start of changerulesEnd of change for DECFLOAT subtraction. If the difference is exactly zero with either sign Start of changeand with the same number of zeroes to the right of the decimal point,End of change the arguments are equal. If a nonzero difference is positive, the first argument is greater than the second argument. If a nonzero difference is negative, the first argument is less than the second.
  • Positive zero and negative zero compare equal.
  • Positive Infinity compares equal to positive infinity.
  • Positive Infinity compares greater than any finite number.
  • Negative Infinity compares equal to negative infinity.
  • Negative Infinity compares less than any finite number.
  • Numeric comparison is exact. The result is determined for finite operands as if range and precision were unlimited. Overflow or underflow cannot occur.
  • If either argument is a NaN or sNaN (positive or negative), the result is unordered.

The result value is set as follows:

0 if the arguments are exactly equal.
1 if expression-1 is less than expression-2.
2 if expression-1 is greater than expression-2.
3 if the arguments are unordered.

The result of the function is SMALLINT. If either argument can be null, the result can be null; if either argument is null, the result is the null value.

Examples

The following examples demonstrate the values that will be returned when the function is used:

     COMPARE_DECFLOAT (DECFLOAT(2.17),  DECFLOAT(2.17))   =  0
     COMPARE_DECFLOAT (DECFLOAT(2.17),  DECFLOAT(2.170))  =  2
     COMPARE_DECFLOAT (DECFLOAT(2.170), DECFLOAT(2.17))   =  1
     COMPARE_DECFLOAT (DECFLOAT(2.17),  DECFLOAT(0.0))    =  2
     COMPARE_DECFLOAT (INFINITY,        INFINITY)         =  0
     COMPARE_DECFLOAT (INFINITY,        -INFINITY)        =  2
     COMPARE_DECFLOAT (DECFLOAT(-2),    INFINITY)         =  1
     COMPARE_DECFLOAT (NAN,             NAN)              =  3
     COMPARE_DECFLOAT (DECFLOAT(-0.1),  SNAN)             =  3