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);
Last updated on