Ticket #7008: div.c.patch

File div.c.patch, 1.2 KB (added by TriEdgeAI, 7 years ago)

Suggests fixes on implementation of div(), ldiv(), lldiv()

  • src/system/libroot/posix/stdlib/div.c

    diff --git a/src/system/libroot/posix/stdlib/div.c b/src/system/libroot/posix/stdlib/div.c
    index 723a315..50b61a0 100644
    a b div(int numerator, int denominator) 
    1616    div_t val;
    1717
    1818    val.quot = numerator / denominator;
    19     val.rem  = numerator - denominator * val.quot;
     19    val.rem  = numerator % denominator;
    2020
    21     if (val.rem > 0 && val.quot < 0) {
     21    if (numerator >= 0 && val.rem < 0) {
    2222        val.rem -= denominator;
    2323        ++val.quot;
    2424    }
    ldiv(long numerator, long denominator) 
    3333    ldiv_t val;
    3434
    3535    val.quot = numerator / denominator;
    36     val.rem  = numerator - denominator * val.quot;
     36    val.rem  = numerator % denominator;
    3737
    38     if (val.rem > 0 && val.quot < 0) {
     38    if (numerator >= 0 && val.rem < 0) {
    3939        val.rem -= denominator;
    4040        ++val.quot;
    4141    }
    lldiv(long long numerator, long long denominator) 
    5050    lldiv_t val;
    5151
    5252    val.quot = numerator / denominator;
    53     val.rem  = numerator - denominator * val.quot;
     53    val.rem  = numerator % denominator;
    5454
    55     if (val.rem > 0 && val.quot < 0) {
     55    if (numerator >= 0 && val.rem < 0) {
    5656        val.rem -= denominator;
    5757        ++val.quot;
    5858    }