As an architect going over memory consistency models it is quite easy to lose focus and direction as to how/where real programs are exposed to these. Some material which seems useful (warning : haven't read/seen the material in entirity yet) :
http://msdn.microsoft.com/en-us/magazine/cc163715.aspx
Wednesday, September 23, 2009
Thursday, September 3, 2009
yipikaye, mothafucka
This is something I could have sweared my life upon, and yet I would have been wrong. Exhibit:
C/C++
Python
What do you expect to be printed out? 0? -1? Turns out, C prints 0 for this, and Python prints -1. I explored more and found this: C assumes the remainder to be the same sign as the dividend (after making the divisor non-zero - otherwise we'd get different answers for -1/2 and 1/-2).
Python always assumes it to be positive. My entire life, I had thought quotient was always floor(a/d), and hence the remainder would always be positive.
The repercussions? Well, this brings a "discontinuity" (I know there's a proper mathematical word for such functions, but lets work with this) in the quotient. 1/2 is also 0, and -1/2 is also 0 because we changed the sign of the remainder. If remainder was always positive, then the function would have been "continuous". How does this affect you? Suppose you were to write a heapify_up function for a binomial heap implemented on an array, where you started at some index, and worked your way to the root. Everyone knows that for a parent node at index i, the children are at 2*i+1 and 2*i+2. Or, for a child j, the parent is at (j-1)/2. Since you want to move up through the parents until you reach the root, you write a loop :
Death 1, Me 0
C/C++
printf("%d\n", -1/2);
Python
print "%d", -1/2
What do you expect to be printed out? 0? -1? Turns out, C prints 0 for this, and Python prints -1. I explored more and found this: C assumes the remainder to be the same sign as the dividend (after making the divisor non-zero - otherwise we'd get different answers for -1/2 and 1/-2).
Python always assumes it to be positive. My entire life, I had thought quotient was always floor(a/d), and hence the remainder would always be positive.
The repercussions? Well, this brings a "discontinuity" (I know there's a proper mathematical word for such functions, but lets work with this) in the quotient. 1/2 is also 0, and -1/2 is also 0 because we changed the sign of the remainder. If remainder was always positive, then the function would have been "continuous". How does this affect you? Suppose you were to write a heapify_up function for a binomial heap implemented on an array, where you started at some index, and worked your way to the root. Everyone knows that for a parent node at index i, the children are at 2*i+1 and 2*i+2. Or, for a child j, the parent is at (j-1)/2. Since you want to move up through the parents until you reach the root, you write a loop :
Pretty clever, eh? As soon as j becomes negative, you break out of the loop. Except, j never becomes negative in C. When j reaches 0, (j-1)/2 evaluates to...drum rolls..0! An effin' infinite loop!
for (j = idx; j >=0; j = (j-1)/2)
{
blah
}
Death 1, Me 0
Subscribe to:
Posts (Atom)