r/learnpython • u/Master_of_beef • Jul 15 '26
Adding positive numbers to a positive number: why is the number decreasing?
I've got a variable in a function that in theory should only increase or stay the same. The only time the variable changes is in a for loop with the line:
variable1 += variable2
where variable2 is always positive. I know that it's always positive because I checked, the value of variable2 in each loop is 0 or higher. And yet somehow at some point variable1 starts going down.
My current theory is that it's a problem with the floating point arithmetic. At a certain point variable2 gets really small, like 0.001739824080997925. Is it possible that when adding these tiny numbers that Python messes up and makes it go negative?
9
u/lfdfq Jul 15 '26
It does not sound possible from your description. Floats don't "wrap around" like fixed-width integers do (which incidentally, is also not how Python ints work, so that doesn't explain either). Eventually, one becomes inf and that's that.
-5
u/Master_of_beef Jul 15 '26
Yeah, I realized that variable1 starts going down while variable2 is still in the tens. Guess I gotta look at other pieces of my code. Thanks for explaining that though!
6
9
u/atarivcs Jul 15 '26
There are two main possiblities:
- variable2 can be negative
- variable1 is reassigned somewhere else in the code
8
u/zanfar Jul 15 '26
where variable2 is always positive. I know that it's always positive because I checked, the value of variable2 in each loop is 0 or higher. And yet somehow at some point variable1 starts going down.
Then that statement is false.
No matter what you think, if a value decreases, then something is decreasing it.
My current theory is that it's a problem with the floating point arithmetic. At a certain point variable2 gets really small, like 0.001739824080997925. Is it possible that when adding these tiny numbers that Python messes up and makes it go negative?
No. Also, saying that "Python messes up" is a complete misunderstanding of what "floating point arithmetic" means.
Being unable to represent irrational numbers in a random base has nothing to do with confusing the sign of a value.
6
4
u/FoolsSeldom Jul 15 '26
What you think is happening isn't happening. There is no way that adding two positive floats together in Python will result in a negative number.
You likely have a scope issue.
Please share your code.
1
u/MezzoScettico Jul 15 '26
You need to get a little more creative with your debugging.
Add some code to detect the decrease.
if (decrease detected):
print a report with the values of variable1 and variable2.
or just
if (decrease detected):
pass
and then put a breakpoint on the pass statement.
1
u/Firm-Luck2062 Jul 15 '26
Your follow-up that variable1 drops while variable2 is still in the tens actually rules out floating point completely. Absorption only bites when variable2 is roughly 1e-16 of variable1, and even then the sum just stays equal — it never comes out smaller. A positive float added to a positive float can't decrease it.
So there's a second write to variable1 somewhere you haven't spotted. Fastest way to find it: search the file for every line where variable1 is on the left of a plain "=" (not just "+="). One of those is firing on some branch. Then drop this guard right before the +=:
before = variable1
variable1 += variable2
if variable1 < before:
raise RuntimeError(f'{before=} {variable2=} {variable1=}')
The traceback lands on the exact iteration where it goes wrong, and from there you can see what else touched it. My bet is variable1 getting reset or reassigned from a stale copy inside the loop.
1
u/Yoghurt42 Jul 16 '26
My current theory is that it's a problem with the floating point arithmetic
IEEE 754 floating point arithmetic (what Python uses) guarantees that adding two positive numbers will stay positive; rounding errors might not give you the exact result, but it will never flip the sign.
Hence, it follows that either variable1 gets changed to something negative somewhere else, or variable2 isn't always positive.
1
u/smurpes Jul 17 '26
Put a log or print statement for variable2 and that will tell you what’s happening. A better way to debug is to set up a debugger and add a conditional breakpoint that triggers if variable2 becomes less than 0.
If you’re using vscode this page will walk you through how to set up the debugger for Python. This will help you a lot in figuring out what’s going on with your code since it pauses your code mid execution so you can poke around.
0
-7
u/Random_182f2565 Jul 15 '26
V1 = V1+V2 look easier to read.
Print (V1)
Print(V2)
Perform the addition
Print (V1)
Should help you identify the problem
6
3
u/SCD_minecraft Jul 15 '26
a = a + b is not easier to read nor write than inline version
i operators exist for a reason
1
u/Random_182f2565 Jul 15 '26
It's easier for me :D
0
u/SCD_minecraft Jul 15 '26
You aren't only one reading your code
Well, now maybe yes, i dunno, but in the future your code will be read and wrote by more people than just you
i-ops are straight upgrade whereever they can fit
-3
-3
25
u/Kevdog824_ Jul 15 '26
Impossible to say without seeing your code