Skip to content

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)
    
    bool test_float_compare_small_small(void) {
        WriteFmt("Testing FloatCompare with small floats\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Float c = FloatFromStr("-1.23", &alloc.base);
    
        bool result = FloatCompare(&a, &b) == 0;
        result      = result && FloatEQ(&a, &b);
        result      = result && (FloatCompare(&c, &a) < 0);
        bool result = FloatCompare(&a, &b) == 0;
        result      = result && FloatEQ(&a, &b);
        result      = result && (FloatCompare(&c, &a) < 0);
        result      = result && (FloatCompare(&a, &c) > 0);
        result      = result && FloatEQ(&a, &b);
        result      = result && (FloatCompare(&c, &a) < 0);
        result      = result && (FloatCompare(&a, &c) > 0);
    
        FloatDeinit(&a);
    
    bool test_float_compare_very_large_large(void) {
        WriteFmt("Testing FloatCompare with very large floats\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        bool result = FloatLT(&a, &b);
        result      = result && FloatGT(&b, &a);
        result      = result && (FloatCompare(&a, &c) == 0);
        result      = result && FloatEQ(&a, &c);
    
    bool test_float_compare_very_large_small(void) {
        WriteFmt("Testing FloatCompare with very large and small floats\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
    bool test_float_compare_generic(void) {
        WriteFmt("Testing FloatCompare generic dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int   next  = IntFrom(13, &alloc.base);
    
        bool result = (FloatCompare(&value, &same) == 0);
        result      = result && (FloatCompare(&value, &whole) > 0);
        result      = result && (FloatCompare(&value, &next) < 0);
    
        bool result = (FloatCompare(&value, &same) == 0);
        result      = result && (FloatCompare(&value, &whole) > 0);
        result      = result && (FloatCompare(&value, &next) < 0);
        result      = result && (FloatCompare(&value, 12) > 0);
        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);
        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);
        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);
        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);
        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);
    
        Float small = FloatFrom((u64)5, &alloc.base);
        int   cmp   = FloatCompare(&small, (u64)100);
        bool  ok    = cmp < 0;
    
        Float value = FloatFrom((u64)42, &alloc.base);
        int   cmp   = FloatCompare(&value, (u64)42);
        bool  ok    = cmp == 0;
    // 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();
        Float b = FloatFrom(0.0, &alloc.base);
    
        int  cmp    = FloatCompare(&a, &b);
        bool result = (cmp > 0);
    // 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();
        Float b = FloatFrom(5.0, &alloc.base);
    
        int  cmp    = FloatCompare(&a, &b);
        bool result = (cmp < 0);
    // 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();
    
        Float lhs = FloatFromStr("5", &alloc.base);
        int   cmp = FloatCompare(&lhs, (u64)3);
    
        bool result = (cmp == 1);
    // 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();
    
        Float lhs = FloatFromStr("5", &alloc.base);
        int   cmp = FloatCompare(&lhs, (u64)5);
    
        bool result = (cmp == 0);
    // 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();
    
        Float lhs = FloatFromStr("5", &alloc.base);
        int   cmp = FloatCompare(&lhs, (u64)9);
    
        bool result = (cmp == -1);
    // 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();
        Float lhs   = FloatFromStr("7", &alloc.base);
        bool  error = true;
        int   cmp   = FloatCompare(&lhs, (u64)2, &error);
    
        bool result = (cmp == 1) && (error == false);
        bool  err = true;
    
        int  cmp = FloatCompare(&a, &b, &err);
        bool ok  = (cmp == -1) && (err == false);
        Float b = FloatFromStr("0", &alloc.base); // zero
    
        int  cmp = FloatCompare(&a, &b);
        bool ok  = (cmp == 1);
        Float b = FloatFromStr("4", &alloc.base); // non-zero
    
        int  cmp = FloatCompare(&a, &b);
        bool ok  = (cmp == -1);
        Float b = FloatFromStr("-2", &alloc.base);
    
        int  cmp = FloatCompare(&a, &b); // -5 < -2 -> -1
        bool ok  = (cmp == -1);
        Float b = FloatFromStr("-5", &alloc.base);
    
        int  cmp = FloatCompare(&a, &b); // -2 > -5 -> 1
        bool ok  = (cmp == 1);
    
        bool error = true;
        int  cmp   = FloatCompare(&lhs, &less, &error);
        bool ok    = (cmp > 0) && (error == false);
    
        error = true;
        cmp   = FloatCompare(&lhs, &same, &error);
        ok    = ok && (cmp == 0) && (error == false);
    
        error = true;
        cmp   = FloatCompare(&lhs, &more, &error);
        ok    = ok && (cmp < 0) && (error == false);
    
        bool error = true;
        int  cmp   = FloatCompare(&lhs, (signed long long)-10, &error);
        bool ok    = (cmp > 0) && (error == false);
    
        error = true;
        cmp   = FloatCompare(&lhs, (signed long long)-4, &error);
        ok    = ok && (cmp == 0) && (error == false);
    
        error = true;
        cmp   = FloatCompare(&lhs, (signed long long)5, &error);
        ok    = ok && (cmp < 0) && (error == false);
    
        bool error = true;
        int  cmp   = FloatCompare(&lhs, 0.25f, &error);
        bool ok    = (cmp > 0) && (error == false);
    
        error = true;
        cmp   = FloatCompare(&lhs, 0.5f, &error);
        ok    = ok && (cmp == 0) && (error == false);
    
        error = true;
        cmp   = FloatCompare(&lhs, 0.75f, &error);
        ok    = ok && (cmp < 0) && (error == false);
        Float b = FloatFromStr("1.25", &dbg.base);
    
        int  cmp = FloatCompare(&a, &b);
        bool ok  = cmp > 0;
        Int            b   = IntFrom(3, &dbg.base);
    
        int  cmp = FloatCompare(&a, &b);
        bool ok  = cmp > 0;
        Float          a   = FloatFromStr("3.5", &dbg.base);
    
        int  cmp = FloatCompare(&a, 3u);
        bool ok  = cmp > 0;
        Float          a   = FloatFromStr("-3.5", &dbg.base);
    
        int  cmp = FloatCompare(&a, -3);
        bool ok  = cmp < 0;
        Float          a   = FloatFromStr("3.5", &dbg.base);
    
        int  cmp = FloatCompare(&a, 3.25f);
        bool ok  = cmp > 0;
        Float          a   = FloatFromStr("3.5", &dbg.base);
    
        int  cmp = FloatCompare(&a, 3.25);
        bool ok  = cmp > 0;
    
        bool result = ZstrCompare(StrBegin(&text), "100") == 0;
        result      = result && (FloatCompare(&value, &whole) == 0);
    
        StrDeinit(&text);
    
        bool result = (FloatExponent(&value) == 0);
        result      = result && (FloatCompare(&value, &whole) == 0);
    
        IntDeinit(&whole);
Last updated on