Re: why do I get this behavior from a while loop?
"S. Chris Colbert" wrote: > >What a newbie mistake for me to make. Don't feel too badly about it. Even very experienced programmers get bitten by this issue. Until someone points it out, it's certainly not obvious. -- Tim Roberts, t...@probo.com Providenza & Boekelheide, Inc. -- http://mail.python.org/mailman/listinfo/python-list
Re: why do I get this behavior from a while loop?
What a newbie mistake for me to make. I appreciate the replies everyone! Cheers, Chris > On Fri, 27 Nov 2009 17:06:44 +0100, S. Chris Colbert wrote: > > I would think that second loop should terminate at 9.9, no? > > > > I am missing something fundamental? > > "What Every Computer Scientist Should Know About Floating Point" > http://docs.sun.com/source/806-3568/ncg_goldberg.html > -- http://mail.python.org/mailman/listinfo/python-list
Re: why do I get this behavior from a while loop?
On Fri, 27 Nov 2009 17:06:44 +0100, S. Chris Colbert wrote: > I would think that second loop should terminate at 9.9, no? > > I am missing something fundamental? "What Every Computer Scientist Should Know About Floating Point" http://docs.sun.com/source/806-3568/ncg_goldberg.html -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: why do I get this behavior from a while loop?
S. Chris Colbert wrote: > In [15]: t = 0. > > In [16]: time = 10. > > In [17]: while t < time: >: print t >: t += 0.1 >: >: > 0.0 > 0.1 > 0.2 > 0.3 > <--snip--> > 9.4 > 9.5 > 9.6 > 9.7 > 9.8 > 9.9 > 10.0 > > > I would think that second loop should terminate at 9.9, no? It would, if a floating point number could represent the number 0.1 and its multiples precisely, but it can't. > I am missing something fundamental? > Yes. Read http://docs.python.org/tutorial/floatingpoint.html . Then, change "print t" to "print repr(t)" to see what's going on. -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list
Re: why do I get this behavior from a while loop?
On Fri, 2009-11-27 at 17:06 +0100, S. Chris Colbert wrote: > This seems strange to me, but perhaps I am just missing something: > I would think that second loop should terminate at 9.9, no? > > I am missing something fundamental? Floating points variables ... http://en.wikipedia.org/wiki/Floating_point 0.1 isn't represented internally, so you haven't actually reached 10. - try comparing that "10." (as it's printed) with 10.0 i.e. print (t == 10.) you'll get False - even though they print the same Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: why do I get this behavior from a while loop?
"S. Chris Colbert" writes: >: print t Try replacing with: print "%0.20f" % t The thing you're missing is that floating point arithmetic isn't (in general) exact - but when it's printed it's rounded. -- http://mail.python.org/mailman/listinfo/python-list
Re: why do I get this behavior from a while loop?
S. Chris Colbert schrieb: This seems strange to me, but perhaps I am just missing something: In [12]: t = 0. In [13]: time = 10. In [14]: while t < time: : print t : t += 1. : : 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 In [15]: t = 0. In [16]: time = 10. In [17]: while t < time: : print t : t += 0.1 : : 0.0 0.1 0.2 0.3 <--snip--> 9.4 9.5 9.6 9.7 9.8 9.9 10.0 I would think that second loop should terminate at 9.9, no? I am missing something fundamental? Yes. The lack of precision that floating points suffer from (nothing python-specific), and the rounding-behavior of printing them. >>> t = 0. >>> time = 10. >>> while t < time: ... print repr(t) ... t += .1 ... 0.0 0.10001 9.8808 9.9805 >>> Diez -- http://mail.python.org/mailman/listinfo/python-list
why do I get this behavior from a while loop?
This seems strange to me, but perhaps I am just missing something: In [12]: t = 0. In [13]: time = 10. In [14]: while t < time: : print t : t += 1. : : 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 In [15]: t = 0. In [16]: time = 10. In [17]: while t < time: : print t : t += 0.1 : : 0.0 0.1 0.2 0.3 <--snip--> 9.4 9.5 9.6 9.7 9.8 9.9 10.0 I would think that second loop should terminate at 9.9, no? I am missing something fundamental? Cheers! Chris -- http://mail.python.org/mailman/listinfo/python-list