Skip to content

IntDivExact

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

Returns

true when the division is exact.

Usage example (Cross-references)

Usage examples (Cross-references)
            u64  places = (u64)(-value->exponent);
            Int  factor = float_pow10(places);
            bool ok     = IntDivExact(&temp, &value->significand, &factor);
    
            IntDeinit(&factor);
    }
    
    bool(IntDivExact)(Int *result, Int *dividend, Int *divisor) {
        ValidateInt(result);
        ValidateInt(dividend);
    bool MISRA_PRIV_IntDivExactU64(Int *result, Int *dividend, u64 divisor) {
        Int  divisor_value = MISRA_PRIV_IntFromU64(divisor);
        bool ok            = IntDivExact(result, dividend, &divisor_value);
    
        IntDeinit(&divisor_value);
    bool MISRA_PRIV_IntDivExactI64(Int *result, Int *dividend, i64 divisor) {
        Int  divisor_value = MISRA_PRIV_IntFromI64(divisor);
        bool ok            = IntDivExact(result, dividend, &divisor_value);
    
        IntDeinit(&divisor_value);
    
    bool test_int_div_exact(void) {
        WriteFmt("Testing IntDivExact generic dispatch\n");
    
        Int dividend = IntFromStr("12345678901234567890");
        Str text = StrInit();
    
        bool result = IntDivExact(&result_value, &dividend, 90u);
        text = IntToStr(&result_value);
        result = result && (strcmp(text.data, "137174210013717421") == 0);
    
    bool test_int_div_exact_failure_preserves_result(void) {
        WriteFmt("Testing IntDivExact failure handling\n");
    
        Int dividend = IntFrom(10);
        Int result_value = IntFrom(99);
    
        bool result = !IntDivExact(&result_value, &dividend, &divisor);
        result      = result && (IntToU64(&result_value) == 99);
    /// TAGS: Int, Math, DivideExact
    ///
    bool (IntDivExact)(Int *result, Int *dividend, Int *divisor);
    ///
    /// Compute `dividend mod divisor`.
                (divisor),                                                                                                 \
                Int: MISRA_PRIV_IntDivExactValue,                                                                          \
                Int *: IntDivExact,                                                                                        \
                const Int *: MISRA_PRIV_IntDivExactConst,                                                                  \
                unsigned char: MISRA_PRIV_IntDivExactU64,                                                                  \
Last updated on