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);
Last updated on