FloatCompare
Description
Compare a float against another numeric value. Dispatches on the type of rhs to the matching internal handler.
Parameters
| Name | Direction | Description |
|---|---|---|
lhs |
in | Left-hand float |
rhs |
in | Right-hand operand (Float, Int, pointer, integer, or native float type) |
error |
out | Optional error flag set to true on operational failure and false otherwise. |
Usage example (from documentation)
int cmp = FloatCompare(&value, 1.5);Success
Returns -1 if lhs < rhs, 0 if equal, 1 if lhs > rhs.
Failure
Returns 0 when an intermediate conversion of rhs to Float fails. With the two-argument form a failure result is indistinguishable from a true equality; with the three-argument form *error is set to true.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Compare.c:42:
bool test_float_compare_small_small(void) {
WriteFmt("Testing FloatCompare with small floats\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:50:
Float c = FloatFromStr("-1.23", &alloc.base);
bool result = FloatCompare(&a, &b) == 0;
result = result && FloatEQ(&a, &b);
result = result && (FloatCompare(&c, &a) < 0);- In
Compare.c:52:
bool result = FloatCompare(&a, &b) == 0;
result = result && FloatEQ(&a, &b);
result = result && (FloatCompare(&c, &a) < 0);
result = result && (FloatCompare(&a, &c) > 0);- In
Compare.c:53:
result = result && FloatEQ(&a, &b);
result = result && (FloatCompare(&c, &a) < 0);
result = result && (FloatCompare(&a, &c) > 0);
FloatDeinit(&a);- In
Compare.c:63:
bool test_float_compare_very_large_large(void) {
WriteFmt("Testing FloatCompare with very large floats\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:73:
bool result = FloatLT(&a, &b);
result = result && FloatGT(&b, &a);
result = result && (FloatCompare(&a, &c) == 0);
result = result && FloatEQ(&a, &c);- In
Compare.c:84:
bool test_float_compare_very_large_small(void) {
WriteFmt("Testing FloatCompare with very large and small floats\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:127:
bool test_float_compare_generic(void) {
WriteFmt("Testing FloatCompare generic dispatch\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:136:
Int next = IntFrom(13, &alloc.base);
bool result = (FloatCompare(&value, &same) == 0);
result = result && (FloatCompare(&value, &whole) > 0);
result = result && (FloatCompare(&value, &next) < 0);- In
Compare.c:137:
bool result = (FloatCompare(&value, &same) == 0);
result = result && (FloatCompare(&value, &whole) > 0);
result = result && (FloatCompare(&value, &next) < 0);
result = result && (FloatCompare(&value, 12) > 0);- In
Compare.c:138:
bool result = (FloatCompare(&value, &same) == 0);
result = result && (FloatCompare(&value, &whole) > 0);
result = result && (FloatCompare(&value, &next) < 0);
result = result && (FloatCompare(&value, 12) > 0);
result = result && (FloatCompare(&value, -1) > 0);- In
Compare.c:139:
result = result && (FloatCompare(&value, &whole) > 0);
result = result && (FloatCompare(&value, &next) < 0);
result = result && (FloatCompare(&value, 12) > 0);
result = result && (FloatCompare(&value, -1) > 0);
result = result && (FloatCompare(&value, 12.5f) == 0);- In
Compare.c:140:
result = result && (FloatCompare(&value, &next) < 0);
result = result && (FloatCompare(&value, 12) > 0);
result = result && (FloatCompare(&value, -1) > 0);
result = result && (FloatCompare(&value, 12.5f) == 0);
result = result && (FloatCompare(&value, 12.5) == 0);- In
Compare.c:141:
result = result && (FloatCompare(&value, 12) > 0);
result = result && (FloatCompare(&value, -1) > 0);
result = result && (FloatCompare(&value, 12.5f) == 0);
result = result && (FloatCompare(&value, 12.5) == 0);
result = result && FloatEQ(&value, &same);- In
Compare.c:142:
result = result && (FloatCompare(&value, -1) > 0);
result = result && (FloatCompare(&value, 12.5f) == 0);
result = result && (FloatCompare(&value, 12.5) == 0);
result = result && FloatEQ(&value, &same);
result = result && FloatEQ(&value, 12.5);- In
Compare.c:253:
Float small = FloatFrom((u64)5, &alloc.base);
int cmp = FloatCompare(&small, (u64)100);
bool ok = cmp < 0;- In
Compare.c:267:
Float value = FloatFrom((u64)42, &alloc.base);
int cmp = FloatCompare(&value, (u64)42);
bool ok = cmp == 0;- In
Compare.c:317:
// both-zero branch and returns 0 instead of the correct +1.
static bool test_m5_compare_nonzero_vs_zero(void) {
WriteFmt("Testing FloatCompare(5.0, 0.0) is strictly greater\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:324:
Float b = FloatFrom(0.0, &alloc.base);
int cmp = FloatCompare(&a, &b);
bool result = (cmp > 0);- In
Compare.c:338:
// the both-zero branch and returns 0 instead of the correct -1.
static bool test_m5_compare_zero_vs_nonzero(void) {
WriteFmt("Testing FloatCompare(0.0, 5.0) is strictly less\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:345:
Float b = FloatFrom(5.0, &alloc.base);
int cmp = FloatCompare(&a, &b);
bool result = (cmp < 0);- In
Compare.c:359:
// real ordering. Asserting the exact ordering value (+1) catches it.
static bool test_m6_compare_u64_greater(void) {
WriteFmt("Testing FloatCompare(Float, u64) returns +1 when greater\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:364:
Float lhs = FloatFromStr("5", &alloc.base);
int cmp = FloatCompare(&lhs, (u64)3);
bool result = (cmp == 1);- In
Compare.c:375:
// Equality path: real code returns 0. A 42 return would fail this exact check.
static bool test_m6_compare_u64_equal(void) {
WriteFmt("Testing FloatCompare(Float, u64) returns 0 when equal\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:380:
Float lhs = FloatFromStr("5", &alloc.base);
int cmp = FloatCompare(&lhs, (u64)5);
bool result = (cmp == 0);- In
Compare.c:391:
// Less-than path: real code returns -1. The mutated return of 42 fails this.
static bool test_m6_compare_u64_less(void) {
WriteFmt("Testing FloatCompare(Float, u64) returns -1 when less\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:396:
Float lhs = FloatFromStr("5", &alloc.base);
int cmp = FloatCompare(&lhs, (u64)9);
bool result = (cmp == -1);- In
Compare.c:409:
// while also pinning the error contract on the success path.
static bool test_m6_compare_u64_no_error_flag(void) {
WriteFmt("Testing FloatCompare(Float, u64, &error) clears error on success\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:415:
Float lhs = FloatFromStr("7", &alloc.base);
bool error = true;
int cmp = FloatCompare(&lhs, (u64)2, &error);
bool result = (cmp == 1) && (error == false);- In
Compare.c:433:
bool err = true;
int cmp = FloatCompare(&a, &b, &err);
bool ok = (cmp == -1) && (err == false);- In
Compare.c:451:
Float b = FloatFromStr("0", &alloc.base); // zero
int cmp = FloatCompare(&a, &b);
bool ok = (cmp == 1);- In
Compare.c:469:
Float b = FloatFromStr("4", &alloc.base); // non-zero
int cmp = FloatCompare(&a, &b);
bool ok = (cmp == -1);- In
Compare.c:487:
Float b = FloatFromStr("-2", &alloc.base);
int cmp = FloatCompare(&a, &b); // -5 < -2 -> -1
bool ok = (cmp == -1);- In
Compare.c:504:
Float b = FloatFromStr("-5", &alloc.base);
int cmp = FloatCompare(&a, &b); // -2 > -5 -> 1
bool ok = (cmp == 1);- In
Compare.c:530:
bool error = true;
int cmp = FloatCompare(&lhs, &less, &error);
bool ok = (cmp > 0) && (error == false);- In
Compare.c:534:
error = true;
cmp = FloatCompare(&lhs, &same, &error);
ok = ok && (cmp == 0) && (error == false);- In
Compare.c:538:
error = true;
cmp = FloatCompare(&lhs, &more, &error);
ok = ok && (cmp < 0) && (error == false);- In
Compare.c:562:
bool error = true;
int cmp = FloatCompare(&lhs, (signed long long)-10, &error);
bool ok = (cmp > 0) && (error == false);- In
Compare.c:566:
error = true;
cmp = FloatCompare(&lhs, (signed long long)-4, &error);
ok = ok && (cmp == 0) && (error == false);- In
Compare.c:570:
error = true;
cmp = FloatCompare(&lhs, (signed long long)5, &error);
ok = ok && (cmp < 0) && (error == false);- In
Compare.c:591:
bool error = true;
int cmp = FloatCompare(&lhs, 0.25f, &error);
bool ok = (cmp > 0) && (error == false);- In
Compare.c:595:
error = true;
cmp = FloatCompare(&lhs, 0.5f, &error);
ok = ok && (cmp == 0) && (error == false);- In
Compare.c:599:
error = true;
cmp = FloatCompare(&lhs, 0.75f, &error);
ok = ok && (cmp < 0) && (error == false);- In
Compare.c:723:
Float b = FloatFromStr("1.25", &dbg.base);
int cmp = FloatCompare(&a, &b);
bool ok = cmp > 0;- In
Compare.c:744:
Int b = IntFrom(3, &dbg.base);
int cmp = FloatCompare(&a, &b);
bool ok = cmp > 0;- In
Compare.c:759:
Float a = FloatFromStr("3.5", &dbg.base);
int cmp = FloatCompare(&a, 3u);
bool ok = cmp > 0;- In
Compare.c:773:
Float a = FloatFromStr("-3.5", &dbg.base);
int cmp = FloatCompare(&a, -3);
bool ok = cmp < 0;- In
Compare.c:787:
Float a = FloatFromStr("3.5", &dbg.base);
int cmp = FloatCompare(&a, 3.25f);
bool ok = cmp > 0;- In
Compare.c:801:
Float a = FloatFromStr("3.5", &dbg.base);
int cmp = FloatCompare(&a, 3.25);
bool ok = cmp > 0;- In
Convert.c:335:
bool result = ZstrCompare(StrBegin(&text), "100") == 0;
result = result && (FloatCompare(&value, &whole) == 0);
StrDeinit(&text);- In
Convert.c:631:
bool result = (FloatExponent(&value) == 0);
result = result && (FloatCompare(&value, &whole) == 0);
IntDeinit(&whole);
Last updated on