Skip to content

FloatDiv

Description

Generic division convenience macro for Float.

Parameters

Name Direction Description
result out Destination for the quotient
a in Dividend
b in Divisor selected through generic dispatch
precision in Decimal precision to retain

Usage example (from documentation)

  FloatDiv(&quotient, &value, 3.0, 8);

Success

Returns true; *result holds a / b with precision decimal digits retained. When a is zero *result is set to zero without further work.

Failure

Returns false when b is zero (logged) or when any intermediate allocation (scale / multiply / divide) fails; *result is unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    
    bool test_float_div_small_small(void) {
        WriteFmt("Testing FloatDiv with small floats\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str   text         = StrInit(&alloc.base);
    
        FloatDiv(&result_value, &a, &b, 3);
        text = FloatToStr(&result_value);
    
    bool test_float_div_very_large_small(void) {
        WriteFmt("Testing FloatDiv with very large and small floats\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str   text         = StrInit(&alloc.base);
    
        FloatDiv(&result_value, &a, &b, 0);
        text = FloatToStr(&result_value);
    
    bool test_float_div_generic(void) {
        WriteFmt("Testing FloatDiv generic dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str   text         = StrInit(&alloc.base);
    
        FloatDiv(&result_value, &a, &b, 1);
        text        = FloatToStr(&result_value);
        bool result = ZstrCompare(StrBegin(&text), "3") == 0;
    
        StrDeinit(&text);
        FloatDiv(&result_value, &a, &whole, 1);
        text   = FloatToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "2.5") == 0);
    
        StrDeinit(&text);
        FloatDiv(&result_value, &a, 3u, 1);
        text   = FloatToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "2.5") == 0);
    
        StrDeinit(&text);
        FloatDiv(&result_value, &a, -3, 1);
        text   = FloatToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "-2.5") == 0);
    
        StrDeinit(&text);
        FloatDiv(&result_value, &a, 0.5f, 1);
        text   = FloatToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "15") == 0);
    
        StrDeinit(&text);
        FloatDiv(&result_value, &a, 0.5, 1);
        text   = FloatToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "15") == 0);
    
    bool test_float_div_by_zero(void) {
        WriteFmt("Testing FloatDiv divide-by-zero handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        bool  ok;
    
        ok = !FloatDiv(&r, &a, &b, 4);
        ok = ok && FloatIsZero(&r);
    // exponent; the lt_to_le mutant aborts -> killed.
    static bool test_m10_div_exp_min_boundary(void) {
        WriteFmt("Testing exponent-diff at INT64_MIN boundary (FloatDiv)\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Float r = FloatInit(&alloc.base);
    
        bool ok = FloatDiv(&r, &a, &b, 0);
        ok      = ok && (FloatExponent(&r) == INT64_MIN);
    // boundary exponent; the gt_to_ge mutant aborts -> killed.
    static bool test_m10_div_exp_max_boundary(void) {
        WriteFmt("Testing exponent-diff at INT64_MAX boundary (FloatDiv)\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Float r = FloatInit(&alloc.base);
    
        bool ok = FloatDiv(&r, &a, &b, 0);
        ok      = ok && (FloatExponent(&r) == INT64_MAX);
        Float r = FloatInit(&alloc.base);
    
        bool ok = !FloatDiv(&r, &a, 0.0, 4);
    
        FloatDeinit(&a);
    
    bool test_m12_div_int_by_zero_returns_false(void) {
        WriteFmt("Testing FloatDiv(Int 0) returns false\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Real float_div rejects division by zero -> false.
        bool ok = FloatDiv(&result, &a, &zero, 4);
    
        bool pass = (ok == false);
    
    bool test_m12_div_i64_by_zero_returns_false(void) {
        WriteFmt("Testing FloatDiv(i64 0) returns false\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Float result = FloatInit(&alloc.base);
    
        bool ok = FloatDiv(&result, &a, (signed long long)0, 4);
    
        bool pass = (ok == false);
    
    bool test_m12_div_f32_by_zero_returns_false(void) {
        WriteFmt("Testing FloatDiv(f32 0) returns false\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Float result = FloatInit(&alloc.base);
    
        bool ok = FloatDiv(&result, &a, 0.0f, 4);
    
        bool pass = (ok == false);
        Str   text     = StrInit(&alloc.base);
    
        bool ok     = FloatDiv(&quotient, &a, &b, 8);
        text        = FloatToStr(&quotient);
        bool result = ok && (ZstrCompare(StrBegin(&text), "2") == 0);
    ///
    bool test_m9_div_u64_by_zero_returns_false(void) {
        WriteFmt("Testing FloatDiv(u64) divide-by-zero returns false\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Float r = FloatInit(&alloc.base);
    
        bool ok = (FloatDiv(&r, &a, 0u, 4) == false);
    
        FloatDeinit(&a);
    ///
    bool test_m9_div_u64_by_zero_leaves_result_unchanged(void) {
        WriteFmt("Testing FloatDiv(u64) divide-by-zero keeps result zero\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Float r = FloatInit(&alloc.base);
    
        bool ok = !FloatDiv(&r, &a, 0u, 4);
        ok      = ok && FloatIsZero(&r);
    ///
    bool test_m9_div_u64_exact(void) {
        WriteFmt("Testing FloatDiv(u64) exact quotient\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str   text;
    
        bool ok = FloatDiv(&r, &a, 3u, 1);
        text    = FloatToStr(&r);
        ok      = ok && (ZstrCompare(StrBegin(&text), "2.5") == 0);
        Float r = FloatFromStr("987654321", &dbg.base); // pre-populated dest
    
        bool ok = FloatDiv(&r, &a, &b, 8u);
    
        FloatDeinit(&a);
        Float r = FloatFromStr("55555", &dbg.base); // pre-populated dest
    
        bool ok = FloatDiv(&r, &a, &b, 4u);
        ok      = ok && FloatIsZero(&r);
        Int            b   = IntFrom(4, &dbg.base);
    
        bool ok = FloatDiv(&r, &a, &b, 6u);
    
        FloatDeinit(&a);
        Float          r   = FloatInit(&dbg.base);
    
        bool ok = FloatDiv(&r, &a, 4u, 6u);
    
        FloatDeinit(&a);
        Float          r   = FloatInit(&dbg.base);
    
        bool ok = FloatDiv(&r, &a, -4, 6u);
    
        FloatDeinit(&a);
        Float          r   = FloatInit(&dbg.base);
    
        bool ok = FloatDiv(&r, &a, 4.0f, 6u);
    
        FloatDeinit(&a);
        Float          r   = FloatInit(&dbg.base);
    
        bool ok = FloatDiv(&r, &a, 4.0, 6u);
    
        FloatDeinit(&a);
Last updated on