Re: Weird result returned from adding floats depending on order I add them

2007-02-21 Thread Steve Holden
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:

Re: Weird result returned from adding floats depending on order I add them

2007-02-21 Thread Rhamphoryncus
On Feb 20, 6:08 am, John Machin [EMAIL PROTECTED] wrote: for x in b: ...tot += x ...print repr(x), repr(tot) ... 0.20001 0.20001 0.20001 0.40002 0.20001 0.60009 0.20001 0.80004

Re: Weird result returned from adding floats depending on order I add them

2007-02-21 Thread Miki
Hello Joanne, ... [float problem] ... I get True returned. Can anyone tell me whats going on and how I can avoid the problem. Thanks If you want to be truly accurate, you can use gmpy.mpq (http:// gmpy.sourceforge.net/). a = [0.2, 0.2, 0.2, 0.1, 0.2, 0.1] b = [0.2, 0.2, 0.2, 0.2, 0.1, 0.1]

Weird result returned from adding floats depending on order I add them

2007-02-20 Thread joanne matthews (RRes-Roth)
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

Re: Weird result returned from adding floats depending on order I add them

2007-02-20 Thread Laurent Pointal
joanne matthews (RRes-Roth) a écrit : 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:

Re: Weird result returned from adding floats depending on order I add them

2007-02-20 Thread John McCallum
Hi, 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 [snip summation] if totalProp != 1: From a numerical analysis point of view, never ever do this. The values you are adding are approximations

Re: Weird result returned from adding floats depending on order I add them

2007-02-20 Thread John Machin
On Feb 20, 11:29 pm, joanne matthews (RRes-Roth) [EMAIL PROTECTED] wrote: I'm getting different results when I add up a list of floats depending on the order that I list the floats. This is quite expected. Floating point arithmetic is subject to rounding errors. [doesn't add to 1.0]

Re: Weird result returned from adding floats depending on order I add them

2007-02-20 Thread Grant Edwards
On 2007-02-20, joanne matthews (RRes-Roth) [EMAIL PROTECTED] wrote: I'm getting different results when I add up a list of floats depending on the order that I list the floats. That's how floats work. For example, the following returns False: def check(): totalProp=0

Re: Weird result returned from adding floats depending on order I add them

2007-02-20 Thread John Machin
On Feb 21, 2:05 am, Grant Edwards [EMAIL PROTECTED] wrote: On 2007-02-20, joanne matthews (RRes-Roth) [EMAIL PROTECTED] wrote: I'm getting different results when I add up a list of floats depending on the order that I list the floats. Don't use floating point if you expect exact results.

Re: Weird result returned from adding floats depending on order I add them

2007-02-20 Thread Grant Edwards
On 2007-02-20, John Machin [EMAIL PROTECTED] wrote: On Feb 21, 2:05 am, Grant Edwards [EMAIL PROTECTED] wrote: On 2007-02-20, joanne matthews (RRes-Roth) [EMAIL PROTECTED] wrote: I'm getting different results when I add up a list of floats depending on the order that I list the floats.

Re: Weird result returned from adding floats depending on order I add them

2007-02-20 Thread John Machin
On Feb 21, 3:44 am, Grant Edwards [EMAIL PROTECTED] wrote: On 2007-02-20, John Machin [EMAIL PROTECTED] wrote: On Feb 21, 2:05 am, Grant Edwards [EMAIL PROTECTED] wrote: On 2007-02-20, joanne matthews (RRes-Roth) [EMAIL PROTECTED] wrote: Don't use floating point if you expect exact

Re: Weird result returned from adding floats depending on order I add them

2007-02-20 Thread Hendrik van Rooyen
joanne matthews (RRes-Roth) [EMAIL PROTECTED] wrote: 8 Can anyone tell me whats going on and how I can avoid the problem. Thanks Don't know about the first question. Would avoid it by using ints and asking for percentages... - Hendrik --