Skip to content

IntDivMod

Description

Generic quotient-and-remainder convenience macro.

Parameters

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

Usage example (from documentation)

  IntDivMod(&q, &r, &value, 97u);

Success

Returns true; *quotient and *remainder jointly satisfy dividend = quotient * divisor + remainder.

Failure

LOG_FATAL if quotient and remainder alias the same object. Returns false when divisor is zero (logged) or when an intermediate allocation fails; *quotient and *remainder are unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    
    bool test_int_div_mod(void) {
        WriteFmt("Testing IntDivMod generic dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str qtext     = StrInit(&alloc.base);
    
        IntDivMod(&quotient, &remainder, &dividend, 97u);
        qtext = IntToStr(&quotient);
    
    bool test_int_div_mod_scalar(void) {
        WriteFmt("Testing IntDivMod scalar-divisor dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Str text      = StrInit(&alloc.base);
    
        IntDivMod(&quotient, &remainder, &dividend, 97);
        text = IntToStr(&quotient);
        Int remainder = IntFrom(77, &alloc.base);
    
        bool result = !IntDivMod(&quotient, &remainder, &dividend, &divisor);
    
        result = result && (IntCompare(&quotient, 99) == 0);
    // faithful mutant yields 42 (truthy). Observe the return value.
    bool test_fe_1539_div_mod_u64_zero_returns_false(void) {
        WriteFmt("Testing IntDivMod u64 zero-divisor return value\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int remainder = IntInit(&alloc.base);
    
        bool ok     = IntDivMod(&quotient, &remainder, &dividend, 0u);
        bool result = (ok == false);
    
    bool test_fe_1552_div_mod_i64_zero_returns_false(void) {
        WriteFmt("Testing IntDivMod i64 zero-divisor return value\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int remainder = IntInit(&alloc.base);
    
        bool ok     = IntDivMod(&quotient, &remainder, &dividend, (i64)0);
        bool result = (ok == false);
        Int r   = IntFrom(9u, a);
    
        bool ok = IntDivMod(&q, &r, &dvd, &dvs);
        ok      = ok && IntToU64(&q) == 1000003u / 101u && IntToU64(&r) == 1000003u % 101u;
        Int r   = IntFrom(9u, a);
    
        bool ok = IntDivMod(&q, &r, &dvd, &dvs);
        ok      = ok && IntIsZero(&q) && IntToU64(&r) == 50u;
        Int m2  = IntFrom(7u, a);
    
        bool ok = IntDivMod(&q, &r, &dvd, 100u);
        ok      = ok && IntMod(&m1, &dvd, 100u);
        ok      = ok && IntMod(&m2, &dvd, (i64)100);
Last updated on