calculate x/y, rounding as appropriate */
| 4548 | |
| 4549 | /* calculate x/y, rounding as appropriate */ |
| 4550 | int |
| 4551 | rounddiv(long x, int y) |
| 4552 | { |
| 4553 | int r, m; |
| 4554 | int divsgn = 1; |
| 4555 | |
| 4556 | if (y == 0) |
| 4557 | panic("division by zero in rounddiv"); |
| 4558 | else if (y < 0) { |
| 4559 | divsgn = -divsgn; |
| 4560 | y = -y; |
| 4561 | } |
| 4562 | if (x < 0) { |
| 4563 | divsgn = -divsgn; |
| 4564 | x = -x; |
| 4565 | } |
| 4566 | r = (int) (x / y); |
| 4567 | m = x % y; |
| 4568 | if (2 * m >= y) |
| 4569 | r++; |
| 4570 | |
| 4571 | return divsgn * r; |
| 4572 | } |
| 4573 | |
| 4574 | |
| 4575 | /*hack.c*/ |