Jump to content
The Official Site of the Vancouver Canucks
Canucks Community

Python programing


uber_pwnzor

Recommended Posts

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...