(foo * 0.0)
always 0.0
?// foo is a double variable. foo *= 0.0; assert(foo == 0.0);
Yes, there is such a case. You can think about it.
I sometimes need to debug some codes from other people. Some of you think this is hard. But it is really depends on who wrote the program. Some of the program, I have fun to debug since I can learn interesting things from the code.
This is a story of my former colleague's code (C.R.). He wrote a
template math class, like Vector< Scaler, int Dim
>
and Matrix< Scalar, N, N >
. (Many
such similar code are there. The problem is no standard...) His
code has a interesting initialization using operator* overloading.
Matrix<float,N,M> a; a *= (Scalar)0.0f;
I was amazed by this code. You do not need to have a code to initialize a matrix by zeros because of nice using of mathematics.
But later, after one coffee time in MPI, another colleague (C.S.) told me an interesting story. He also wrote similar code on Windows, and his debug version works as expected. But once he optimized his code, the code did not work anymore. He tracked down the code, but, debugger did not tell anything. I was surprised since I thought (at least at the 2005) debugger tool is the one that the free software on Linux's is behind from a commercial debugging tool on Windows.
Also, when he changed the code some part without affect anything
around math code, like put a cout
, the bug is gone.
Finally he realized that when one of the element of vector is initialized coincidentally NaN. This is the difference between the theory and a practice.
I was surprised again. I thought C.R.'s code was so cool, but there was a pitfall! This is both the language and environment dependent.
This story does not end here. I later asked him about this, but I had not finished the story. He said, ``Ja, but it's not always correct, I was just lazy and it should be fixed. Sorry for that.'' So, he knew this problem. I learned many things at the institute from the colleagues not only the computer science.