Hlavní navigace

Názor k článku BigInt: nový celočíselný typ v JavaScriptu od Josef Pavlik - ano, SAFE_MULDIV funguje i pro zaporna cisla #define SAFE_MULDIV(A,B,C)...

  • 20. 6. 2019 16:59

    Josef Pavlik

    ano, SAFE_MULDIV funguje i pro zaporna cisla


    #define SAFE_MULDIV(A,B,C) /* A*B/C */ ((A)/(C)*(B)+­(A)%(C)*(B)/(C))
    #include <stdio.h>

    int main()
    {
    printf(" 3_000 x 10_000_00 / 1_000 = %d\n", SAFE_MULDIV( 3000, 1000000, 1000));
    printf("-3_000 x 10_000_00 / 1_000 = %d\n", SAFE_MULDIV(-3000, 1000000, 1000));
    printf(" 3_000 x 10_000_00 /-1_000 = %d\n", SAFE_MULDIV( 3000, 1000000,-1000));
    printf(" 3_000 x-10_000_00 / 1_000 = %d\n", SAFE_MULDIV( 3000,-1000000, 1000));
    }
    $ ./x
    3_000 x 10_000_00 / 1_000 = 3000000
    -3_000 x 10_000_00 / 1_000 = -3000000
    3_000 x 10_000_00 /-1_000 = -3000000
    3_000 x-10_000_00 / 1_000 = -3000000