RG <[email protected]> writes:
[...]
> That the problem is "elsewhere in the program" ought to be small
> comfort.
I don't claim that it's comforting, merely that it's true.
> But very well, try this instead:
>
> [...@mighty:~]$ cat foo.c
> #include <stdio.h>
>
> int maximum(int a, int b) { return a > b ? a : b; }
>
> int main() {
> long x = 8589934592;
> printf("Max of %ld and 1 is %d\n", x, maximum(x,1));
> return 0;
> }
> [...@mighty:~]$ gcc -Wall foo.c
> [...@mighty:~]$ ./a.out
> Max of 8589934592 and 1 is 1
That exhibits a very similar problem.
8589934592 is 2**33.
Given the output you got, I presume your system has 32-bit int and
64-bit long. The call maximum(x, 1) implicitly converts the long
value 8589934592 to int. The result is implementation-defined,
but typically 0. So maximum() is called with arguments of 0 and 1,
as you could see by adding a printf call to maximum().
Even here, maximum() did exactly what was asked of it.
I'll grant you that having a conversion from a larger type to a smaller
type quietly discard high-order bits is unfriendly. But it matches the
behavior of most CPUs.
Here's another example:
#include <stdio.h>
int maximum(int a, int b) { return a > b ? a : b; }
int main(void) {
double x = 1.8;
printf("Max of %f and 1 is %d\n", x, maximum(x, 1));
return 0;
}
Output:
Max of 1.800000 and 1 is 1
--
Keith Thompson (The_Other_Keith) [email protected] <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
http://mail.python.org/mailman/listinfo/python-list