uber_pwnzor Posted July 9, 2012 Share Posted July 9, 2012 Hey. So I'm on this like ten hour bus ride (forgot my cell phone at home but remembered my computer). I'm pretty bored so I decided to try some Python programing, I've never done this before, so I'm very bad, and I was wondering if someone could help me out a bit: I came up with the idea that finding out the solution to an equation wouldn't be too hard, so I started: with a simple one (2x=10; x=5). >>> x=1 >>> while x*2!=10: x=x+1 print x 2 3 4 5 >>> That worked out really well, but if I write: >>> x=1 >>> while x*2!=10: x=x+0.1 print x Python will just keep on going... Why is this? Link to comment Share on other sites More sharing options...
AbbyNucksFan Posted July 9, 2012 Share Posted July 9, 2012 what do you mean by "keep on going..." what does it output? Link to comment Share on other sites More sharing options...
avelanch Posted July 9, 2012 Share Posted July 9, 2012 i think he means it doesn't stop at 5.0 I think it may be something to do with the fact that the calc output of 2*5.0 is 10.0 and not 10, so it thinks the output doesn't meet the criteria and just keeps going, producing an infinite loop. I've never done anything in python, so i don't know if this is indeed the problem, but i know I've hit issues similar to this in other languages. Link to comment Share on other sites More sharing options...
Canucks_4_Cup Posted July 9, 2012 Share Posted July 9, 2012 Once you start dealing with floats things get get a little bit iffy. Computers can never really represent exact float values. When you check the equality "x*2 != 10" where x = 5.0, you would expect to get "false" but in a while loop where x is the sum of a bunch of floats, you will run into some trouble. I hope that's clear. Link to comment Share on other sites More sharing options...
uber_pwnzor Posted July 9, 2012 Author Share Posted July 9, 2012 i think he means it doesn't stop at 5.0 I think it may be something to do with the fact that the calc output of 2*5.0 is 10.0 and not 10, so it thinks the output doesn't meet the criteria and just keeps going, producing an infinite loop. I've never done anything in python, so i don't know if this is indeed the problem, but i know I've hit issues similar to this in other languages. Link to comment Share on other sites More sharing options...
Tortorella's Rant Posted July 9, 2012 Share Posted July 9, 2012 And I was going to take programming at school.. Not a chance in hell that's happening now. Terrible at math and I don't even know what I'm looking at. Networking all the way Link to comment Share on other sites More sharing options...
uber_pwnzor Posted July 9, 2012 Author Share Posted July 9, 2012 Once you start dealing with floats things get get a little bit iffy. Computers can never really represent exact float values. When you check the equality "x*2 != 10" where x = 5.0, you would expect to get "false" but in a while loop where x is the sum of a bunch of floats, you will run into some trouble. I hope that's clear. Link to comment Share on other sites More sharing options...
uber_pwnzor Posted July 9, 2012 Author Share Posted July 9, 2012 what do you mean by "keep on going..." what does it output? Link to comment Share on other sites More sharing options...
Canucks_4_Cup Posted July 9, 2012 Share Posted July 9, 2012 Try using "< 10" instead of "!= 10" Also, to break out of an infinite loop, press ctrl+C Link to comment Share on other sites More sharing options...
uber_pwnzor Posted July 9, 2012 Author Share Posted July 9, 2012 Try using "< 10" instead of "!= 10" Also, to break out of an infinite loop, press ctrl+C Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.