torsdag 19 maj 2016

In Python, -k/2 != -(k/2)

I just found an interesting fact about Python. The following C program:
#include

int main(int argc, char * argv[]){
int k = 11;
printf("k = %d\n", k);
printf("k/2 = %d\n", k/2);
printf("-k/2 = %d\n", -k/2);
printf("-(k/2) = %d\n", -(k/2));
}
will output:
k = 11
k/2 = 5
-k/2 = -5
-(k/2) = -5

However, the following Python program:
k = 11
print k
print k/2
print -k/2
print -(k/2)
will output:
11
5
-6
-5

I guess this is related to Pythons floor division, and has apparently been changed in Python 3.

Inga kommentarer: