B
Bertrand Mollinier Toublet
All,
consider the following piece of code:
#include <stdio.h>
int main(int argc, char **argv) {
unsigned int foo = 2026363600u + argc - 1;
printf("off2 = %u\n", (838237499u * foo - 2137600414u) % 11u);
return 0;
}
Depending on the compiler used, the output varies, and I am not sure
whether a given output should be expected, or if the varying result is
actually to be expected: the "trick" here is that both the the
multiplicative and additive coefficients in the computation in the
printf() statement are multiples of 11.
Therefore, theoretically, the value passed to printf should be 0 in all
cases.
Having said that, on an implementation where unsigned int is 32 bit
(e.g. a x86 compatible), if (838237499u * foo - 2137600414u) is first
adjusted for overflow, then the result of the modulo operation is 2, not
zero.
So. Given all this, I have seen some compilers return 0 unconditionally,
some compilers returning 2 unconditionally, and I am worried about the
variance. Any insight?
consider the following piece of code:
#include <stdio.h>
int main(int argc, char **argv) {
unsigned int foo = 2026363600u + argc - 1;
printf("off2 = %u\n", (838237499u * foo - 2137600414u) % 11u);
return 0;
}
Depending on the compiler used, the output varies, and I am not sure
whether a given output should be expected, or if the varying result is
actually to be expected: the "trick" here is that both the the
multiplicative and additive coefficients in the computation in the
printf() statement are multiples of 11.
Therefore, theoretically, the value passed to printf should be 0 in all
cases.
Having said that, on an implementation where unsigned int is 32 bit
(e.g. a x86 compatible), if (838237499u * foo - 2137600414u) is first
adjusted for overflow, then the result of the modulo operation is 2, not
zero.
So. Given all this, I have seen some compilers return 0 unconditionally,
some compilers returning 2 unconditionally, and I am worried about the
variance. Any insight?