I have 2 int's. How do I divide one by the other and then round up afterwards?
When doing an integer division in c++, the result will always be rounded down, so your rounding system isn't off :) For example even if you divide 1999 by 1000, the result will be 1 if both 1999 and 1000 are integers.
If the divisor and dividend have the same sign then the result is zero or positive. If the divisor and dividend have opposite signs then the result is zero or negative. If the division is inexact then the quotient is rounded up.
Yes, the result is always truncated towards zero. It will round towards the smallest absolute value.
If your ints are A
and B
and you want to have ceil(A/B) just calculate (A+B-1)/B
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With