rounding of 4 unsigned char numbers

A

asutic

Hi. I've encountered a problem with rounding which I cannot resolve
elegantly and I has hoping that somebody could help me out. Here's the
problem description:

We are given 4 numbers a, b, c, and d in the range [0..255]. These are
8-bit numbers (unsigned char), but I assume that the solution should
exists for any non-negative integer. We are supposed to determine x =
x(a, b, c, d) so that the following 2 formulas give identical results:

(a + b + c + d + 2) >> 2 = (((a + c + 1) >> 1) + ((b + d + 1) >> 1) +
x) >> 1

This means that the ceiling of quarter sum of 4 numbers should be
accomplished in 2 steps.

Similar formula should be applied for flooring of quarter sum of 4
numbers:

(a + b + c + d) >> 2 = (((a + c) >> 1) + ((b + d) >> 1) + x) >> 1

Any ideas how can we do this elegantly? It looks like the solution
inspects if a, b, c, d are odd or even, plus if they are divisible by
4.

Thanks in advance for your help! I'd really appreciate it.

Regards,
Aleksandar
 
A

asutic

Hi. I've encountered a problem with rounding which I cannot resolve
elegantly and I has hoping that somebody could help me out. Here's the
problem description:

We are given 4 numbers a, b, c, and d in the range [0..255]. These are
8-bit numbers (unsigned char), but I assume that the solution should
exists for any non-negative integer. We are supposed to determine x =
x(a, b, c, d) so that the following 2 formulas give identical results:

(a + b + c + d + 2) >> 2 = (((a + c + 1) >> 1) + ((b + d + 1) >> 1) +
x) >> 1

This means that the ceiling of quarter sum of 4 numbers should be
accomplished in 2 steps.

Similar formula should be applied for flooring of quarter sum of 4
numbers:

(a + b + c + d) >> 2 = (((a + c) >> 1) + ((b + d) >> 1) + x) >> 1

Any ideas how can we do this elegantly? It looks like the solution
inspects if a, b, c, d are odd or even, plus if they are divisible by
4.

Thanks in advance for your help! I'd really appreciate it.

Regards,
Aleksandar

Well, eventually I came to a fairly elegant solution:

int main() {
int a, b, c, d, ref, mdl, error = 0, x, tmp, i;

for (i=0; i<10000; i++) {
a = (int) (256.0 * (rand() / (RAND_MAX + 1.0)));
b = (int) (256.0 * (rand() / (RAND_MAX + 1.0)));
c = (int) (256.0 * (rand() / (RAND_MAX + 1.0)));
d = (int) (256.0 * (rand() / (RAND_MAX + 1.0)));

tmp = (a & 3) + (b & 3) + (c & 3) + (d & 3);
x = (tmp == 2 || tmp == 6 || tmp == 7 || tmp == 10) ? 1 : 0;

ref = (a + b + c + d + 2) >> 2;
mdl = (((a + c + 1) >> 1) + ((b + d + 1) >> 1) + x) >> 1;

if (ref != mdl) {
error++;
printf("FAILED: a = %d, b = %d, c = %d, d = %d, (ref - mdl)
= %2d, x = %d\n",
a, b, c, d, ref - mdl, x);
}
}

printf("Total errors for ceiling (random): %d\n", error);

error = 0;

for (i=0; i<10000; i++) {
a = (int) (256.0 * (rand() / (RAND_MAX + 1.0)));
b = (int) (256.0 * (rand() / (RAND_MAX + 1.0)));
c = (int) (256.0 * (rand() / (RAND_MAX + 1.0)));
d = (int) (256.0 * (rand() / (RAND_MAX + 1.0)));

tmp = (a & 3) + (b & 3) + (c & 3) + (d & 3);
x = (tmp == 1 || tmp == 4 || tmp == 8 || tmp == 9) ? 1 : 0;

ref = (a + b + c + d) >> 2;
mdl = (((a + c) >> 1) + ((b + d) >> 1) + x) >> 1;

if (ref != mdl) {
error++;
printf("FAILED: a = %d, b = %d, c = %d, d = %d, (ref -
mdl) = %2d, x = %d\n",
a, b, c, d, ref - mdl, x);
}
}

printf("Total errors for flooring (random): %d\n", error);

return 0;
}

Regards,
Aleksandar
 
H

Herbert Kleebauer

asutic said:
Well, eventually I came to a fairly elegant solution:

int main() {
int a, b, c, d, ref, mdl, error = 0, x, tmp, i;

for (i=0; i<10000; i++) {
a = (int) (256.0 * (rand() / (RAND_MAX + 1.0)));
b = (int) (256.0 * (rand() / (RAND_MAX + 1.0)));
c = (int) (256.0 * (rand() / (RAND_MAX + 1.0)));
d = (int) (256.0 * (rand() / (RAND_MAX + 1.0)));

tmp = (a & 3) + (b & 3) + (c & 3) + (d & 3);
x = (tmp == 2 || tmp == 6 || tmp == 7 || tmp == 10) ? 1 : 0;


Why not: x= !(((a+c)|(b+d))&1);
or: x= (((a^c)|(b^d))^1)&1;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top