473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

rounding of 4 unsigned char numbers

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
Aug 1 '08 #1
2 1702
On Aug 1, 9:30*am, asutic <asu...@gmail.comwrote:
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
Aug 1 '08 #2
asutic wrote:
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;
Aug 1 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
8
by: archilleswaterland | last post by:
hello, is there any way that I can represent FF (11111111) as an unsigned char ? unsigned char ch; so if the user enters FF and i have printf("%c",ch); it should spit out FF
4
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok...
12
by: Martin Wells | last post by:
I'm trying to come up with a fully-portable macro for supplying memset with an unsigned char rather than an int. I'm going to think out loud as I go along. . . I'll take a sample system before I...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
11
by: john | last post by:
Hi, at first the code doesn't seem to work. Any ideas?: #include <iostream> #include <cstdlib> int main() { using namespace std;
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.