This is usually the reason for the bug.
But there are multiple ways to approach this.
I feel that in software development, we have lost an "engineering culture". In an engineering culture, you work carefully, and try to understand: you need both an experimental and a theoretical approach. Yet too many programmers work by just changing something, and if it passes the test, they think they are done.
But they don't really understand the code.
There is no substitute for understanding.
I personally work by writing the algorithm - not by coding. I mentally verify the algorithm. Then I translate that into code. I find that if I do that, there are no bugs - none. And I don't need tests to "define" the internal behavior, because the algorithm defines it. So my tests can be at the outer behavioral level, instead of the internal code level.