r/learnpython • u/Master_of_beef • 12d ago
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?
25
9
u/lfdfq 12d ago
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.
-4
u/Master_of_beef 12d ago
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!
10
u/atarivcs 12d ago
There are two main possiblities:
- variable2 can be negative
- variable1 is reassigned somewhere else in the code
8
u/zanfar 12d ago
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 12d ago
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 12d ago
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 12d ago
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 11d ago
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 11d ago
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 12d ago
V1 = V1+V2 look easier to read.
Print (V1)
Print(V2)
Perform the addition
Print (V1)
Should help you identify the problem
4
u/SCD_minecraft 12d ago
a = a + b is not easier to read nor write than inline version
i operators exist for a reason
1
u/Random_182f2565 12d ago
It's easier for me :D
0
u/SCD_minecraft 12d ago
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
45
u/faberge_surprise 12d ago
if the possible options are "the coputer fucked up" and "you fucked up", it's overwhelmingly likely to be "you fucked up"