Just spent the last 2 hours tracking down a bug that ended up being super simple, thought I'd share my frustrating experience and maybe save someone the trouble.
I had my recursive function running and my triangle was mostly correct, but some of the numbers were off. I couldn't figure out why, it worked on paper, worked on my calculator, worked when I tested those numbers in a separate piece of code. Why wouldn't it work in my function? I assumed I either had a mistake in my formula, or my code was rounding off somewhere.
The piece of code ended up being subtle, but simple. I was using:
number *= x/y; // as an example, number starts at 2, is multiplied by 1/2, should be 1, right?
Wrong, it was rounding to 0. Ok, but if I printed a test line as:
2*1/2 I got 1. So why was I still getting 0 in my function?
I finally realized that in this case, n*= is not the same as n = n*. The order of operations rounds 1/2 down to 0, then does 1*0=0. I did not think about it, but I should have realized it would resolve both sides separately, and of course round down because x and y are both ints.
So anyway, there's how I spent the last 2 hours. It did not feel very rewarding, but I understand my mistake now and will hopefully avoid relearning it so painfully next time. And on the plus side, once that was fixed the rest of my program is working nicely.
Anyone else have a painful learning experience on this one? Hope to see a lot of you in class tomorrow, it should be a good catch-up day.