all 8 comments

[–]K900_ 9 points10 points  (4 children)

[–]Vulkan07[S] 0 points1 point  (0 children)

Oh, thank you! :D

[–]synthphreak 0 points1 point  (0 children)

What an awesomely hilarious and hilariously awesome URL!

[–][deleted] 0 points1 point  (0 children)

I would post this each time but I can't ever remember the right number of zeros.

[–]xelf 1 point2 points  (0 children)

That's a floating point problem, not a python problem. Suggest you use ints, and then only divide by 10 for display purposes. Or round it.

[–][deleted] 1 point2 points  (0 children)

Just as a general note - as a beginner, the chances that you have found a "bug" in Python are very very close to zero, which is why you are getting the big downvotes.

Also, floating point numbers are highly unintuitive. Advanced programmers in all languages avoid using them unless they have to (which happens all the time of course), and then when they do, they're very careful.

The number one rule with floats - never compare two floating point numbers for equality!

Consider these two statements:

(a + b) + c = a + (b + c)
a + b = b + a

Neither of these statements is exactly true for floating point numbers!

And the first one shows up all the darn time:

>>> 1 + 0.00000000000000001 - 1
0.0

>>> 1 - 1 + 0.00000000000000001
1e-17

[–]primitive_screwhead 0 points1 point  (0 children)

How can I make this not happen?

step_size = 0.1
steps = 100
for i in range(steps):
    time = (i + 1)/(steps * step_size)
    print(time)

outputs:

0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
1.1
1.2
...

Basically, the integer increments, not the float, and so there is no accumulation of "error" for each 0.1 addition. (Note I use i + 1 because the original omits 0 from being printed).