Skip to content

IntDiv

Description

Generic 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)

  IntDiv(&quotient, &value, 10u);

Success

Returns true; *result holds the floor quotient of dividend / divisor.

Failure

Returns false when divisor is zero (logged) or when an intermediate allocation fails; *result is unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    
    bool test_int_div(void) {
        WriteFmt("Testing IntDiv generic dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntInit(&alloc.base);
    
        IntDiv(&result_value, &dividend, 10u);
    
        bool result = IntToU64(&result_value) == 12;
    
    bool test_int_div_scalar_zero_divisor(void) {
        WriteFmt("Testing IntDiv scalar zero-divisor handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int quotient = IntFrom(99, &alloc.base);
    
        IntDiv(&quotient, &dividend, 0u);
        bool result = IntCompare(&quotient, 99) == 0;
    
        // 1000 / 7 == 142 (floor).
        bool ok     = IntDiv(&quotient, &dividend, (i64)7);
        bool result = ok;
        result      = result && (IntToU64(&quotient) == 142u);
        Int quotient = IntFrom(55, &alloc.base);
    
        bool failed = !IntDiv(&quotient, &dividend, (i64)-7);
        bool result = failed;
    
        /* Unsigned literal forces int_div_u64 dispatch. */
        bool ok     = IntDiv(&result_value, &dividend, 7u);
        bool result = ok && (IntToU64(&result_value) == 14);
    // (42). The quotient is left unchanged on failure.
    bool test_fe_1487_div_u64_zero_returns_false(void) {
        WriteFmt("Testing IntDiv u64 zero-divisor return value\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int quotient = IntFrom(99, &alloc.base);
    
        bool ok     = IntDiv(&quotient, &dividend, 0u);
        bool result = (ok == false);
        result      = result && (IntCompare(&quotient, 99) == 0);
    
    bool test_fe_1500_div_i64_zero_returns_false(void) {
        WriteFmt("Testing IntDiv i64 zero-divisor return value\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int quotient = IntFrom(99, &alloc.base);
    
        bool ok     = IntDiv(&quotient, &dividend, (i64)0);
        bool result = (ok == false);
        result      = result && (IntCompare(&quotient, 99) == 0);
        Int r   = IntFrom(7u, a);
    
        bool ok = IntDiv(&r, &dvd, &dvs); // int_div frees remainder on success (1445)
        ok      = ok && IntToU64(&r) == 1000003u / 101u;
        Int r3  = IntFrom(7u, a);
    
        bool ok = IntDiv(&r1, &dvd, 8u);
        ok      = ok && IntDivExact(&r2, &dvd, 8u);
        ok      = ok && IntDiv(&r3, &dvd, (i64)8);
        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;
Last updated on