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:
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
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]
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
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:
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
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]
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
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.
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.
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
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
--
12 matches
Mail list logo