Skip to content

IntDivExact

Description

Generic exact-division convenience macro.

Parameters

Name Direction Description
result out Destination for the quotient
dividend in Dividend
divisor in Divisor selected through generic dispatch

Usage example (from documentation)

  bool ok = IntDivExact(&quotient, &value, 5u);

Success

Returns true when the division is exact.

Failure

Returns false otherwise.

Usage example (Cross-references)

Usage examples (Cross-references)
    
    bool test_int_div_exact(void) {
        WriteFmt("Testing IntDivExact generic dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str text         = StrInit(&alloc.base);
    
        bool result = IntDivExact(&result_value, &dividend, 90u);
        text        = IntToStr(&result_value);
        result      = result && (ZstrCompare(StrBegin(&text), "137174210013717421") == 0);
    
    bool test_int_div_exact_failure_preserves_result(void) {
        WriteFmt("Testing IntDivExact failure handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntFrom(99, &alloc.base);
    
        bool result = !IntDivExact(&result_value, &dividend, &divisor);
        result      = result && (IntToU64(&result_value) == 99);
        Int divisor  = IntFrom(4, &alloc.base);
    
        IntDivExact(NULL, &dividend, &divisor);
    
        IntDeinit(&dividend);
    
        // 1001 / 7 == 143 exactly.
        bool ok     = IntDivExact(&quotient, &dividend, (i64)7);
        bool result = ok;
        result      = result && (IntToU64(&quotient) == 143u);
        Int quotient = IntFrom(55, &alloc.base);
    
        bool failed = !IntDivExact(&quotient, &dividend, (i64)-7);
        bool result = failed;
    // divisible by 10, so real code returns false, mutant returns true.
    bool test_fe_1513_div_exact_u64_inexact_false(void) {
        WriteFmt("Testing IntDivExact u64 inexact return value\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int quotient = IntInit(&alloc.base);
    
        bool ok     = IntDivExact(&quotient, &dividend, 10u);
        bool result = (ok == false);
    
    bool test_fe_1526_div_exact_i64_inexact_false(void) {
        WriteFmt("Testing IntDivExact i64 inexact return value\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int quotient = IntInit(&alloc.base);
    
        bool ok     = IntDivExact(&quotient, &dividend, (i64)10);
        bool result = (ok == false);
        Int r   = IntFrom(7u, a);
    
        bool ok = IntDivExact(&r, &dvd, &dvs); // frees remainder on success (1474)
        ok      = ok && IntToU64(&r) == 125u;
    
        bool ok = IntDiv(&r1, &dvd, 8u);
        ok      = ok && IntDivExact(&r2, &dvd, 8u);
        ok      = ok && IntDiv(&r3, &dvd, (i64)8);
        ok      = ok && IntToU64(&r1) == 125u && IntToU64(&r2) == 125u && IntToU64(&r3) == 125u;
        Int r   = IntFrom(7u, a);
    
        bool ok = IntDivExact(&r, &dvd, (i64)8);
        ok      = ok && IntToU64(&r) == 125u;
Last updated on