Search code examples
pythonsubtraction

subraction error


I have a certain error in a speed testing program I have built in Python. My program works by taking a variable of 0, adding 1 and printing its value. It repeats this 10,000 times, and records how long this all takes. After the time is taken every 1,000 cycle, it is arranged into a graph, with the columns, "Cycles", "Time", and "Time between previous checkpoint". The time between the previous checkpoint remains normal until it comes to 5 and 6 thousand cycles. It would look like this;

enter image description here

Here is a snippet of the table code:

print("   4,000        %ss                  %ss" % (check4, check4 - check3))
print("|          |            |                                  |")
print("   5,000        %ss                  %ss" % (check5, check5 - check4))
print("|          |            |                                  |")
print("   6,000        %ss                  %ss" % (check6, check6 - check5))

As you can see, the results spike then dip. The problem is, they shouldn't. It wouldn't make sense that there can be a negative amount of time between two events. I've checked where the checkpoint variables are assigned and they are like this;

    elif(count == 4000):
        check4 = round(time() - start, 3)
        check4 -= 0.3
        Beep(825, 100)
        print("        %s        %ss" % (count, round(time() - start, 2)))
    elif(count == 5000):
        check5 = round(time() - start, 3)
        check5 -= - 0.4
        Beep(850, 100)
        print("        %s        %ss" % (count, round(time() - start, 2)))
    elif(count == 6000):
        check6 = round(time() - start, 3)
        check6 -= 0.5
        Beep(875, 100)
        print("        %s        %ss" % (count, round(time() - start, 2)))

There is either something I've overlooked because I'm looking for something else, or my method is at fault. I've only come here as a last resort, I've been slaving over this on and off for at least 3 months.

If anyone could find what is causing this value anomaly, please could they respond.


Solution

  • In your second elif statement in your code snippet, it looks like you have a minus sign that should not be there:

    elif(count == 5000):
            check5 = round(time() - start, 3)
            check5 -= [-] 0.4
            Beep(850, 100)
    

    This makes your program add 0.4 rather than subtract, which seems to be the source of your error.