-7.275957614183426e-12 means -7.275957614183426 * 10^{-12} ~ -0.000000000007, which is just very small negative number. You can detect it by comparing with another very small fixed (of your choice) number.
You can write this pseudocode it in python easily (I don't know python):
epsilon = 0.000001 (for example)
is_almost_zero(x) =
... if (-epsilon < x < epsilon) return true
... else return false
I have a short program written in Python (I'm running v3.7) that inputs values from a file, adds or subtracts from a running balance and checks the float-value of that running balance, maintained by the program, with that held in the input file.
Simple enough you'd think and up until today it's worked nicely but after updating my data file and running the figures through the program, I came across this unexpected result...
41389.46 + 1.70 yielded the result 41391.159999999996
this isn't a problem and reflects the fact that decimal values can't always be represented precisely by binary-based systems. When I need to display such a value I specify two decimal places and that's usually fine.
But the next line caused the problem...
41391.159999999996 - 41391.16 yields -7.275957614183426e-12
The expected balance is 0.00 but the following formatting of -7.275957614183426e-12 gives this bizarre (to me) result...
f'{-7.275957614183426e-12:.2f}' gives -0.00 (minus zero!)
of course when my program compares -0.00 with 0.00, it detects a mismatch.
It looks like a possible oversight (if not a bug?) within Python but is there any easy way I can correct this, preferably without the use of additional modules? I've been looking at the Python Decimal module but I'm not sure yet if the solution is in there.