Math How to divide an odd number to leave two integers?

Math How to divide an odd number to leave two integers?,math,Math,If I have an odd number, how would I divide it in two and leave two integers, with the first being one more than the second. For instance 9 would produce 5 and 4?

If I have an odd number, how would I divide it in two and leave two integers, with the first being one more than the second. For instance 9 would produce 5 and 4?

**#1**

The "smaller half" of `int x`

is `x/2`

. The "bigger half" is `x/2 + x%2`

or `x - x/2`

.

Note that "smaller" and "bigger" refer to the absolute value, so in the case of negative `x`

, `bigger < smaller`

.

Of course, if `x`

is always odd and positive, then `x%2`

will be `1`

and the bigger half can also be computed as `x/2 + 1`

.

**#2**

What about this?

```
int a = 9;
int c = a/2;
int b = a-c;
```

**#3**

This would be my recommended way:

```
int low = floor(x / 2.0f);
int high = ceil(x / 2.0f);
```

I find it to be more concise than the `x/2 + x%2`

version.
This version also benefits from the fact that the output will be correct if you happen to run it using an even number.

EDIT:

People seemed to complain about me using floating point for integers, well here is a completely bitwise based version:

```
int a = 9;
int b = a >> 1;
int c = b | (a & 0x1);
```

The only caveat with #2 is that if the input is negative, the results will not be what is expected.

**#4**

For the folks who use microcontrollers, where `/`

and `%`

are *fearsome-cost* operations :-)

This shows an alternative method, using shift `>>`

and `&`

which are sometimes cheaper:

```
#include <stdio.h>
int main (int argc, const char * argv[]) {
const int iplus = 9;
const int iminus = -9;
printf("iplus=%d iminus=%d\n", iplus, iminus);
printf("(iplus >> 1)=%d ((iplus >> 1) + (iplus & 1))=%d\n", iplus >> 1, (iplus >> 1) + (iplus & 1));
printf("(iminus >> 1)=%d ((iminus >> 1) + (iminus & 1))=%d\n", iminus >> 1, (iminus >> 1) + (iminus & 1));
return 0;
}
```

Output:

```
iplus=9 iminus=-9
(iplus >> 1)=4 ((iplus >> 1) + (iplus & 1))=5
(iminus >> 1)=-5 ((iminus >> 1) + (iminus & 1))=-4
```

According to this Does either ANSI C or ISO C specify what -5 % 10 should be?

There is a difference of behaviour for `/`

between C89 and C99, and specifically C89 `'/`

with one negative number may return a positive or negative result, but C99 is negative.

**#5**

I thought the accepted answer was in the ballpark but unclear. If you want some copy and paste code this would be the best solution in my eyes

```
var number = 11;
var halfRoundedUp = (number % 2) ? number/2 + .5 : number/2;
var halfRoundedDown = (number % 2) ? number/2 - .5 : number/2;
alert(halfRoundedUp +" "+ halfRoundedDown);
```

**#6**

`a / 2`

and `a / 2 + 1`

?
**#7**

**#8**

`x - x/2`

would be somewhat simpler for the second part, given that you just calculated `x/2`

;-)
**#9**

`x%2`

into a register access.
**#10**

**#11**

**#12**

`x / 2`

and `x >> 1`

are different
**#13**

**#14**

**#15**

**#16**

`If I have an odd number`

. So, the results should be `x / 2`

if it's an even number.
**#17**

**#18**

`floor`

and `ceil`

with floating point input. Your inputs are integers.
**#19**

**#20**

`double`

operations. What if the code is to be executed on an embedded microcontroller with no FPU and no `math.h`

?
**#21**

**#22**