LibC: Make div() and ldiv() behave according to the C standard

This commit is contained in:
Andreas Kling 2019-09-27 10:03:26 +02:00
parent b009f8522c
commit 0b59c0d0dc
Notes: sideshowbarker 2024-07-19 11:59:04 +09:00

View File

@ -348,6 +348,11 @@ div_t div(int numerator, int denominator)
div_t result;
result.quot = numerator / denominator;
result.rem = numerator % denominator;
if (numerator >= 0 && result.rem < 0) {
result.quot++;
result.rem -= denominator;
}
return result;
}
@ -356,6 +361,11 @@ ldiv_t ldiv(long numerator, long denominator)
ldiv_t result;
result.quot = numerator / denominator;
result.rem = numerator % denominator;
if (numerator >= 0 && result.rem < 0) {
result.quot++;
result.rem -= denominator;
}
return result;
}