joanne matthews (RRes-Roth) wrote: > I'm getting different results when I add up a list of floats depending > on the order that I list the floats. For example, the following returns > False: > def check(): > totalProp=0 > inputs=[0.2,0.2,0.2,0.1,0.2,0,0.1] > for each in inputs: > > totalProp+=each > print "totalProp=",totalProp > if totalProp != 1: > print "Your proportions must add up to 1" > > return False > return True > > However, if I swap, the 4th and 5th list items like this: > > totalProp=0 > inputs=[0.2,0.2,0.2,0.2,0.1,0,0.1] > for each in inputs: > > totalProp+=each > print "totalProp=",totalProp > if totalProp != 1: > print "Your proportions must add up to 1" > > return False > return True > > I get True returned. Can anyone tell me whats going on and how I can > avoid the problem. Thanks > > Joanne Matthews
I take it by now you understand that you need to test that the sum of your numbers is with some small delta of the value you actually require? I seem to remember this question has been asked on a number of threads, just don't know whether this is the same question as the others. regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://del.icio.us/steve.holden Blog of Note: http://holdenweb.blogspot.com See you at PyCon? http://us.pycon.org/TX2007 -- http://mail.python.org/mailman/listinfo/python-list