Skip to content

FloatAdd

Description

Generic addition convenience macro for Float.

Parameters

Name Direction Description
result out Destination for the sum
a in Left operand
b in Right operand selected through generic dispatch

Usage example (from documentation)

  FloatAdd(&sum, &value, 1.25);

Success

Returns true; *result holds a + b with shared exponent and normalized significand.

Failure

Returns false if an intermediate allocation (clone / rescale / add) fails; *result is unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
        Str   t = StrInit(&alloc.base);
    
        FloatAdd(&r, &a, &b);
        t       = FloatToStr(&r);
        bool ok = ZstrCompare(StrBegin(&t), "100.1") == 0;
        Str   t = StrInit(&alloc.base);
    
        FloatAdd(&r, &a, &z);
        t       = FloatToStr(&r);
        bool ok = ZstrCompare(StrBegin(&t), "12.5") == 0;
    
    bool test_float_add_small_small(void) {
        WriteFmt("Testing FloatAdd with small floats\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str   text         = StrInit(&alloc.base);
    
        FloatAdd(&result_value, &a, &b);
        text = FloatToStr(&result_value);
    
    bool test_float_add_very_large_large(void) {
        WriteFmt("Testing FloatAdd with very large floats\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str   text         = StrInit(&alloc.base);
    
        FloatAdd(&result_value, &a, &b);
        text = FloatToStr(&result_value);
    
    bool test_float_add_generic(void) {
        WriteFmt("Testing FloatAdd generic dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str   text         = StrInit(&alloc.base);
    
        FloatAdd(&result_value, &a, &b);
        text        = FloatToStr(&result_value);
        bool result = ZstrCompare(StrBegin(&text), "2") == 0;
    
        StrDeinit(&text);
        FloatAdd(&result_value, &a, &whole);
        text   = FloatToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "3.25") == 0);
    
        StrDeinit(&text);
        FloatAdd(&result_value, &a, 2u);
        text   = FloatToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "3.25") == 0);
    
        StrDeinit(&text);
        FloatAdd(&result_value, &a, -1);
        text   = FloatToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "0.25") == 0);
    
        StrDeinit(&text);
        FloatAdd(&result_value, &a, 0.75f);
        text   = FloatToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "2") == 0);
    
        StrDeinit(&text);
        FloatAdd(&result_value, &a, 0.75);
        text   = FloatToStr(&result_value);
        result = result && (ZstrCompare(StrBegin(&text), "2") == 0);
    // Real: -2 + 5 = 3. Mutant: -3.
    bool test_m1_add_neg_lhs_larger_pos_rhs(void) {
        WriteFmt("Testing FloatAdd sign: -2 + 5 = 3\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str   text         = StrInit(&alloc.base);
    
        FloatAdd(&result_value, &a, &b);
        text = FloatToStr(&result_value);
    // the mutant; kept for behavioral coverage of the cmp<0 branch.
    bool test_m1_add_pos_lhs_larger_neg_rhs(void) {
        WriteFmt("Testing FloatAdd sign: 2 + (-5) = -3\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str   text         = StrInit(&alloc.base);
    
        FloatAdd(&result_value, &a, &b);
        text = FloatToStr(&result_value);
    // yield -0.75.
    bool test_m1_add_sign_from_larger_magnitude(void) {
        WriteFmt("Testing FloatAdd sign w/ fractions: -0.25 + 1 = 0.75\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str   text         = StrInit(&alloc.base);
    
        FloatAdd(&result_value, &a, &b);
        text = FloatToStr(&result_value);
        Float r = FloatInit(&dbg.base);
    
        bool ok = FloatAdd(&r, &a, &b);
    
        FloatDeinit(&a);
        Float r = FloatFromStr("123456789", &dbg.base); // pre-populated dest
    
        bool ok = FloatAdd(&r, &a, &b);
    
        FloatDeinit(&a);
        Float r = FloatInit(&dbg.base);
    
        bool ok = FloatAdd(&r, &a, &b);
        ok      = ok && FloatIsZero(&r);
        Int            b   = IntFrom(2, &dbg.base);
    
        bool ok = FloatAdd(&r, &a, &b);
    
        FloatDeinit(&a);
        Float          r   = FloatInit(&dbg.base);
    
        bool ok = FloatAdd(&r, &a, 2u);
    
        FloatDeinit(&a);
        Float          r   = FloatInit(&dbg.base);
    
        bool ok = FloatAdd(&r, &a, -2);
    
        FloatDeinit(&a);
        Float          r   = FloatInit(&dbg.base);
    
        bool ok = FloatAdd(&r, &a, 0.5f);
    
        FloatDeinit(&a);
        Float          r   = FloatInit(&dbg.base);
    
        bool ok = FloatAdd(&r, &a, 0.25);
    
        FloatDeinit(&a);
Last updated on