Message from discussion
Python comparing float
Received: by 10.43.8.201 with SMTP id ot9mr7993181icb.10.1346181961145;
Tue, 28 Aug 2012 12:26:01 -0700 (PDT)
X-BeenThere: pygame-mirror-on-google-groups@googlegroups.com
Received: by 10.50.11.232 with SMTP id t8ls8913674igb.2.canary; Tue, 28 Aug
2012 12:26:00 -0700 (PDT)
Received: by 10.50.34.197 with SMTP id b5mr7559476igj.5.1346181960958;
Tue, 28 Aug 2012 12:26:00 -0700 (PDT)
Received: by 10.50.34.197 with SMTP id b5mr7559475igj.5.1346181960940;
Tue, 28 Aug 2012 12:26:00 -0700 (PDT)
Return-Path: <owner-pygame-us...@seul.org>
Received: from moria.seul.org (moria.csail.mit.edu. [128.31.0.34])
by gmr-mx.google.com with ESMTPS id dd6si1523272igc.0.2012.08.28.12.26.00
(version=TLSv1/SSLv3 cipher=OTHER);
Tue, 28 Aug 2012 12:26:00 -0700 (PDT)
Received-SPF: neutral (google.com: 128.31.0.34 is neither permitted nor denied by best guess record for domain of owner-pygame-us...@seul.org) client-ip=128.31.0.34;
Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 128.31.0.34 is neither permitted nor denied by best guess record for domain of owner-pygame-us...@seul.org) smtp.mail=owner-pygame-us...@seul.org; dkim=pass header...@gmail.com
Received: by moria.seul.org (Postfix)
id 445BE10475466; Tue, 28 Aug 2012 15:25:59 -0400 (EDT)
Delivered-To: pygame-users-outgo...@seul.org
Received: by moria.seul.org (Postfix, from userid 65534)
id 3579F105928FE; Tue, 28 Aug 2012 15:25:59 -0400 (EDT)
X-Original-To: pygame-us...@seul.org
Delivered-To: pygame-us...@seul.org
Received: from mail-vc0-f182.google.com (mail-vc0-f182.google.com [209.85.220.182])
(using TLSv1 with cipher RC4-SHA (128/128 bits))
(No client certificate requested)
by moria.seul.org (Postfix) with ESMTPS id F0FA710475466
for <pygame-us...@seul.org>; Tue, 28 Aug 2012 15:25:58 -0400 (EDT)
Received: by vcbgb22 with SMTP id gb22so8071357vcb.13
for <pygame-us...@seul.org>; Tue, 28 Aug 2012 12:25:58 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20120113;
h=mime-version:in-reply-to:references:date:message-id:subject:from:to
:content-type;
bh=HPzj/rjn6ATO1PU7BHBylbV+yIEAU8cTLfTwhq1Z0Po=;
b=Vu9V+pWJj+FwIxcudxX+HtBif06G2q07u0SH+gToeXRxbcT+hc+hSIjM+K/ilLYbCI
zMvlEygDy8A7WyzqZMmuqYCS8b9bak0ikgkvN/XBHpi2iMlhceRiWipwe+ZW03q1aLC8
thcE2wVRuFmuwuaTTCvGc1k5NXO1ZQq+lR+k1HRawGJ+H6Xee0GipebgCahjwCvSDuEE
H8BEG0sqUIrcyawZh0Fs/BDtWTBrVmzjaK3yqOYwHxIm55sevviNn17kyQsWbakDBfkk
Rm0/3KpiUejss1wpYmY9XNPwYqQxnPDoAE1t2Ej4vfzuIj0O++4GIf9iMjT5kcAiPy8Z
SRyQ==
MIME-Version: 1.0
Received: by 10.220.150.211 with SMTP id z19mr14680765vcv.48.1346181958107;
Tue, 28 Aug 2012 12:25:58 -0700 (PDT)
Received: by 10.58.91.163 with HTTP; Tue, 28 Aug 2012 12:25:58 -0700 (PDT)
In-Reply-To: <CAFpHyEuRFrQ69ja3Sj7GHerVP18kuhDAdbASFgenOT2Et6g...@mail.gmail.com>
References: <CAFpHyEtPCXapw6T1x2+uG=uz5=2+drx+Bx1BdtTND58d5KM...@mail.gmail.com>
<CAJ2VPS054_=ngTSLHRf9EVdjKZbB_6eHcB4BN2rk_LRofnZ...@mail.gmail.com>
<CAFpHyEstamJ8YLna1UKz0PKALBZJdmo7ucBVXHcxBv+GiP_...@mail.gmail.com>
<503594F7.9050...@canterbury.ac.nz>
<CAFpHyEuRFrQ69ja3Sj7GHerVP18kuhDAdbASFgenOT2Et6g...@mail.gmail.com>
Date: Tue, 28 Aug 2012 20:25:58 +0100
Message-ID: <CA+OHizw-KfghjrodtgYfqdmu7eM6Qk3Ccz-QfZUbnmBJKdX...@mail.gmail.com>
Subject: Re: [pygame] Python comparing float
From: Russell Jones <russell.jo...@gmail.com>
To: pygame-us...@seul.org
Content-Type: multipart/alternative; boundary=f46d043c82246a817004c858697d
Sender: owner-pygame-us...@seul.org
Precedence: list
Reply-To: pygame-us...@seul.org
X-To-Get-Off-This-List: mail majord...@seul.org, body unsubscribe pygame-users
--f46d043c82246a817004c858697d
Content-Type: text/plain; charset=UTF-8
Alternatively, in the numpy module (part of the Scientific Tools for Python
http://www.scipy.org/ ) there is the linspace function, which you could use
like this
for x in numpy.linspace(0,2,21):
print(x)
Russell
On 23 August 2012 14:34, Ricardo Franco <ricardo.kr...@gmail.com> wrote:
> Well, this is a good idea, I go do this way. tks
>
>
> 2012/8/22 Greg Ewing <greg.ew...@canterbury.ac.nz>
>
>> Ricardo Franco wrote:
>>
>>> That's why when you are comparing floating point numbers, you always
>>> have to check if they are within some small error value from each
>>> other. How large that value is depends on your particular use case.
>>>
>>
>> It's even better if you can avoid comparing floats for
>> equality at all. You can do that in this case by using an
>> integer loop counter and computing the float value from it:
>>
>> >>> for i in xrange(11):
>> ... rate = 1.0 + i * 0.1
>> ... print rate
>> ...
>> 1.0
>> 1.1
>> 1.2
>> 1.3
>> 1.4
>> 1.5
>> 1.6
>> 1.7
>> 1.8
>> 1.9
>> 2.0
>>
>> --
>> Greg
>>
>
>
>
> --
> Ricardo Franco Andrade @ricardokrieg
>
> ( Game | Desktop | Web ) Developer
>
> email: ricardo.kr...@gmail.com
> contact: +55 (86) 9958 9725
> +55 (86) 9436 0830
> twitter: twitter.com/ricardokrieg
> facebooK: https://www.facebook.com/krieg.ricardo
> github: https://github.com/ricardokrieg
>
>
--f46d043c82246a817004c858697d
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Alternatively, in the numpy module (part of the Scientific Tools for Python=
=C2=A0<a href=3D"http://www.scipy.org/">http://www.scipy.org/</a>=C2=A0) th=
ere is the linspace function, which you could use like this<div>for x in nu=
mpy.linspace(0,2,21):</div>
<div>=C2=A0 =C2=A0 print(x)<br><br>Russell<br><br><div class=3D"gmail_quote=
">On 23 August 2012 14:34, Ricardo Franco <span dir=3D"ltr"><<a href=3D"=
mailto:ricardo.kr...@gmail.com" target=3D"_blank">ricardo.kr...@gmail.com</=
a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div>Well, this is a good idea, I go do this=
way. tks<div><div class=3D"h5"><br><br><div class=3D"gmail_quote">2012/8/2=
2 Greg Ewing <span dir=3D"ltr"><<a href=3D"mailto:greg.ewing@canterbury.=
ac.nz" target=3D"_blank">greg.ew...@canterbury.ac.nz</a>></span><br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div>Ricardo Franco wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
=C2=A0 =C2=A0 That's why when you are comparing floating point numbers,=
you always<br>
=C2=A0 =C2=A0 have to check if they are within some small error value from =
each<br>
=C2=A0 =C2=A0 other. How large that value is depends on your particular use=
case.<br>
</blockquote>
<br></div>
It's even better if you can avoid comparing floats for<br>
equality at all. You can do that in this case by using an<br>
integer loop counter and computing the float value from it:<br>
<br>
>>> for i in xrange(11):<br>
... =C2=A0 =C2=A0rate =3D 1.0 + i * 0.1<br>
... =C2=A0 =C2=A0print rate<br>
...<br>
1.0<br>
1.1<br>
1.2<br>
1.3<br>
1.4<br>
1.5<br>
1.6<br>
1.7<br>
1.8<br>
1.9<br>
2.0<span><font color=3D"#888888"><br>
<br>
-- <br>
Greg<br>
</font></span></blockquote></div><br><br clear=3D"all"><div><br></div></div=
></div><div class=3D"im">-- <br>Ricardo Franco Andrade=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 @ricardokrieg<br><br>( =
Game | Desktop | Web ) Developer<br><br>email: <a href=3D"mailto:ricardo.kr=
i...@gmail.com" target=3D"_blank">ricardo.kr...@gmail.com</a><br>
contact:=C2=A0<a href=3D"tel:%2B55%20%2886%29%209958%209725" value=3D"+5586=
99589725" target=3D"_blank">+55 (86) 9958 9725</a><div>=C2=A0 =C2=A0 =C2=A0=
=C2=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"tel:%2B55%20%2886%29%209436%200830" =
value=3D"+558694360830" target=3D"_blank">+55 (86) 9436 0830</a>=C2=A0<br>
twitter: <a href=3D"http://twitter.com/ricardokrieg" target=3D"_blank">twit=
ter.com/ricardokrieg</a><br><div>facebooK:=C2=A0<a href=3D"https://www.face=
book.com/krieg.ricardo" target=3D"_blank">https://www.facebook.com/krieg.ri=
cardo</a></div>
</div><div>github:=C2=A0<a href=3D"https://github.com/ricardokrieg" target=
=3D"_blank">https://github.com/ricardokrieg</a></div><br>
</div></div>
</blockquote></div><br></div>
--f46d043c82246a817004c858697d--