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

You mean a / 2 and a / 2 + 1?

#7

do you want the expressions to produce the correct answer if 1. the number is even (or will it never be needed?), and 2. if the number is negative?

#8

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

#9

@DavidHeffernan: good point, added it. I do believe that integer division on the x86 always produces the modulus in a register as well, so a smart compiler might optimize the x%2 into a register access.

#10

Unlikely that performance is that relevant and you still have to perform an addition, just as per my version.

#11

@mouviciel: "this is just a shift right" -- assuming an unsigned type. There's a little more to it for signed types because of round-towards-zero. In this example, we might know that the value will always be positive and so it doesn't matter, but if the compiler doesn't know that then it can't make that transformation even if it wants to.

#12

@mouviciel for negative integers, x / 2 and x >> 1 are different

#13

But if a is even, the result is not what is wanted.

#14

@Richard Number is odd according to question.

#15

The spec indicates that a is odd.

#16

@DavidHeffernan the question states If I have an odd number. So, the results should be x / 2 if it's an even number.

#17

@RichardJ.RossIII The question states that the input is odd. Read it again.

#18

-1 You use floor and ceil with floating point input. Your inputs are integers.

#19

Not if that's the best asking you can do. And not if you insist on using floating point for a problem that should be performed purely with integer arithmetic.

#20

Integer arithmetic should not use double operations. What if the code is to be executed on an embedded microcontroller with no FPU and no math.h?

#21

@RichardJ.RossIII I don't care about performance, it's just pointless to use FP math for the most trivial integer arithmetic imaginable. If I would never allow this code past code review.

#22

Bit twiddling is even worse! Just use proper arithmetic operators for the love of god! This is the most trivial programming problem imaginable.