IntCompare
Description
Compare an integer against another integer-like value. Dispatches on the type of rhs to the matching internal handler.
Parameters
| Name | Direction | Description |
|---|---|---|
lhs |
in | Left-hand integer |
rhs |
in | Right-hand operand (Int, pointer, u64, or i64 compatible type) |
Usage example (from documentation)
int cmp = IntCompare(&value, 42);Success
Returns -1 if lhs < rhs, 0 if equal, 1 if lhs > rhs.
Failure
Pure comparison cannot fail. LOG_FATAL if either operand pointer is NULL (propagated from the underlying handler’s ValidateInt).
Usage example (Cross-references)
Usage examples (Cross-references)
bool test_int_compare_ignores_leading_zeros(void) {
WriteFmt("Testing IntCompare leading-zero normalization\n");
DefaultAllocator alloc = DefaultAllocatorInit(); Int rhs = IntFrom(11, ALLOCATOR_OF(&alloc));
bool result = IntCompare(&lhs, &rhs) == 0;
result = result && IntEQ(&lhs, &rhs);- In
Int.Compare.c:17:
bool test_int_compare(void) {
WriteFmt("Testing IntCompare\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Int.Compare.c:25:
Int c = IntFromBinary("000101010", &alloc.base);
bool result = IntCompare(&a, &b) < 0;
result = result && (IntCompare(&b, &a) > 0);
result = result && (IntCompare(&b, &c) == 0);- In
Int.Compare.c:26:
bool result = IntCompare(&a, &b) < 0;
result = result && (IntCompare(&b, &a) > 0);
result = result && (IntCompare(&b, &c) == 0);- In
Int.Compare.c:27:
bool result = IntCompare(&a, &b) < 0;
result = result && (IntCompare(&b, &a) > 0);
result = result && (IntCompare(&b, &c) == 0);
IntDeinit(&a);- In
Int.Compare.c:62:
bool test_int_compare_generic(void) {
WriteFmt("Testing IntCompare generic dispatch\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Int.Compare.c:72:
IntShiftLeft(&big, 80);
bool result = (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, 42) == 0);- In
Int.Compare.c:73:
bool result = (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, 42) == 0);
result = result && (IntCompare(&value, 100ULL) < 0);- In
Int.Compare.c:74:
bool result = (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, 42) == 0);
result = result && (IntCompare(&value, 100ULL) < 0);
result = result && (IntCompare(&value, -1) > 0);- In
Int.Compare.c:75:
result = result && (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, 42) == 0);
result = result && (IntCompare(&value, 100ULL) < 0);
result = result && (IntCompare(&value, -1) > 0);
result = result && (IntCompare(&big, UINT64_MAX) > 0);- In
Int.Compare.c:76:
result = result && (IntCompare(&value, 42) == 0);
result = result && (IntCompare(&value, 100ULL) < 0);
result = result && (IntCompare(&value, -1) > 0);
result = result && (IntCompare(&big, UINT64_MAX) > 0);
result = result && IntEQ(&value, 42);- In
Int.Compare.c:77:
result = result && (IntCompare(&value, 100ULL) < 0);
result = result && (IntCompare(&value, -1) > 0);
result = result && (IntCompare(&big, UINT64_MAX) > 0);
result = result && IntEQ(&value, 42);
result = result && IntLE(&value, 42);- In
Int.Math.c:753:
IntModMul(&check, &result_value, &b, &m);
result = result && (IntCompare(&check, 10) == 0);
IntDeinit(&a);- In
Int.Math.c:842:
bool result = IntModSqrt(&root, &value, &mod);
IntSquareMod(&check, &root, &mod);
result = result && (IntCompare(&check, 10) == 0);
IntDeinit(&value);- In
Int.Math.c:862:
bool result = !IntModSqrt(&root, &value, &mod);
result = result && (IntCompare(&root, 99) == 0);
IntDeinit(&value);- In
Int.Math.c:939:
bool result = !IntModDiv(&result_value, &a, &b, &m);
result = result && (IntCompare(&result_value, 99) == 0);
IntDeinit(&a);- In
Int.Math.c:984:
bool result = !IntDivMod("ient, &remainder, ÷nd, &divisor);
result = result && (IntCompare("ient, 99) == 0);
result = result && (IntCompare(&remainder, 77) == 0);- In
Int.Math.c:985:
result = result && (IntCompare("ient, 99) == 0);
result = result && (IntCompare(&remainder, 77) == 0);
IntDeinit(÷nd);- In
Int.Math.c:1005:
bool result = !IntRootRem(&root, &remainder, &value, 0);
result = result && (IntCompare(&root, 99) == 0);
result = result && (IntCompare(&remainder, 77) == 0);- In
Int.Math.c:1006:
result = result && (IntCompare(&root, 99) == 0);
result = result && (IntCompare(&remainder, 77) == 0);
IntDeinit(&value);- In
Int.Math.c:1024:
IntDiv("ient, ÷nd, 0u);
bool result = IntCompare("ient, 99) == 0;
IntDeinit(÷nd);- In
Int.Math.c:1041:
IntMod(&result_value, &value, 0u);
bool result = IntCompare(&result_value, 99) == 0;
IntDeinit(&value);- In
Int.Math.c:1060:
bool result = !IntModDiv(&result_value, &a, &b, &m);
result = result && (IntCompare(&result_value, 99) == 0);
IntDeinit(&a);- In
Int.Math.c:1116:
bool result = !IntPowMod(&result_value, &base, &exp, &mod);
result = result && (IntCompare(&result_value, 99) == 0);
IntDeinit(&base);- In
Compare.h:106:
/// TAGS: Int, Compare, Equal, Generic
///
# define IntEQ(lhs, rhs) (IntCompare((lhs), (rhs)) == 0)
///
/// Test whether `lhs` is strictly less than `rhs`.
- In
Compare.h:121:
/// TAGS: Int, Compare, LessThan, Generic
///
# define IntLT(lhs, rhs) (IntCompare((lhs), (rhs)) < 0)
///
/// Test whether `lhs` is less than or equal to `rhs`.
- In
Compare.h:136:
/// TAGS: Int, Compare, LessEqual, Generic
///
# define IntLE(lhs, rhs) (IntCompare((lhs), (rhs)) <= 0)
///
/// Test whether `lhs` is strictly greater than `rhs`.
- In
Compare.h:151:
/// TAGS: Int, Compare, GreaterThan, Generic
///
# define IntGT(lhs, rhs) (IntCompare((lhs), (rhs)) > 0)
///
/// Test whether `lhs` is greater than or equal to `rhs`.
- In
Compare.h:166:
/// TAGS: Int, Compare, GreaterEqual, Generic
///
# define IntGE(lhs, rhs) (IntCompare((lhs), (rhs)) >= 0)
///
/// Test whether two numeric values differ.
- In
Compare.h:181:
/// TAGS: Int, Compare, NotEqual, Generic
///
# define IntNE(lhs, rhs) (IntCompare((lhs), (rhs)) != 0)
#endif
Last updated on