Skip to content

IntMod

Description

Generic modulo convenience macro.

Parameters

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

Usage example (from documentation)

  IntMod(&remainder, &value, 97u);

Success

Returns true; *result holds dividend mod divisor in the range [0, |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_mod(void) {
        WriteFmt("Testing IntMod generic dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntInit(&alloc.base);
    
        IntMod(&result_value, &dividend, 10u);
    
        bool result = IntToU64(&result_value) == 6;
    
    bool test_int_mod_scalar(void) {
        WriteFmt("Testing IntMod scalar-divisor dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int remainder = IntInit(&alloc.base);
    
        IntMod(&remainder, &value, 97u);
    
        IntDeinit(&value);
    
    bool test_int_mod_scalar_zero_modulus(void) {
        WriteFmt("Testing IntMod scalar zero-modulus handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntFrom(99, &alloc.base);
    
        IntMod(&result_value, &value, 0u);
        bool result = IntCompare(&result_value, 99) == 0;
        Int result   = IntInit(&alloc.base);
    
        bool ok      = IntMod(&result, &dividend, (unsigned long)7u);
        bool correct = ok && (IntToU64(&result) == (u64)2u);
    // false; the faithful mutant yields 42 (truthy). Observe the return value.
    bool test_fe_1606_mod_u64_zero_returns_false(void) {
        WriteFmt("Testing IntMod u64 zero-divisor return value\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntInit(&alloc.base);
    
        bool ok     = IntMod(&result_value, &dividend, 0u);
        bool result = (ok == false);
    
    bool test_fe_1614_mod_i64_zero_returns_false(void) {
        WriteFmt("Testing IntMod i64 zero-divisor return value\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntInit(&alloc.base);
    
        bool ok     = IntMod(&result_value, &dividend, (i64)0);
        bool result = (ok == false);
    
        bool ok = IntDivMod(&q, &r, &dvd, 100u);
        ok      = ok && IntMod(&m1, &dvd, 100u);
        ok      = ok && IntMod(&m2, &dvd, (i64)100);
        ok      = ok && IntToU64(&q) == 10u && IntToU64(&r) == 3u;
        bool ok = IntDivMod(&q, &r, &dvd, 100u);
        ok      = ok && IntMod(&m1, &dvd, 100u);
        ok      = ok && IntMod(&m2, &dvd, (i64)100);
        ok      = ok && IntToU64(&q) == 10u && IntToU64(&r) == 3u;
        ok      = ok && IntToU64(&m1) == 3u && IntToU64(&m2) == 3u;
        Int r   = IntFrom(7u, a);
    
        bool ok = IntMod(&r, &dvd, &dvs); // int_mod frees quotient on success (1598)
        ok      = ok && IntToU64(&r) == 3u;
Last updated on