[Tutor] Re: Why x+=y instead of x=x+y?

Derrick 'dman' Hudson dman@dman.ddts.net
Sat, 20 Jul 2002 22:30:00 -0500


--ADZbWkCsHQ7r3kzd
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Jul 19, 2002 at 01:57:27PM +0100, alan.gauld@bt.com wrote:
| > > 	x +=3D 0.01
| > Now I know this is shorthand for "x =3D x+0.01" and "x =3D=20
| > x+step", but I have always wondered what the point is=20
|=20
| It comres from C which was initially implemented on a=20
| computer which had a different set of assembler instructions=20
| for generic addition and 'self addition' Thus +=3D etc=20
| actually produced faster code. (For the same reason x++ still=20
| does because many assemblers have an INC X command for=20
| incrementing which is faster than ADD X 1)

I think it is also related to the complexity of the compiler.  I
expect that any modern compiler will turn "x+=3D1" into INC X.  I
assume, though, that early compilers were not as sophisticated, and if
they had been it would have been painful to use them on the hardware
of the day.

| > Or, to put it differently: Is this just another example of a=20
| > construct that was included in Python to make the "Waaah,=20
| > I wanna keep my C style,=20

Now in python it can still make a difference in performance because,
for example, lists are capable of performing in-place modifications.
So
    l1 =3D l1 + l2
causes the original data in l1 to be copied to a new location, the
data from l2 to be appended, and then the result returned and the
original l1 freed.  However
    l1 +=3D l2
allows l1 to simply stick the l2 data in the existing memory slots
(assuming it has enough "spares").


Coming back to the original question of using a float increment in a
range, it won't work even if you try to code it yourself.

step_count =3D 0
i =3D 0 ;
step =3D .1
while i < 10 :
    i +=3D step
    step_count +=3D 1

print "Total steps :", step_count


So how many iterations will you have, going from 0 to 10 with .1
intervals?  100?  No. 101!

Don't believe me?  Try it!  That's why range() only works on integers
-- it only works for integers.  The reason is the same as why you
don't use floats when dealing with exact data like money.  floats are
_great_ for high-speed data of either large or small magnitude where
"close" is close enough.  Particularly for scientific type work where
the precision of the floats is several orders of magnitude better than
the precision of your measuring tools.

-D

--=20
There are six things the Lord hates,
    seven that are detestable to him :
        haughty eyes,
        a lying tongue,
        hands that shed innocent blood,
        a heart that devises wicked schemes,
        feet that are quick to rush into evil,
        a false witness who pours out lies
        and a man who stirs up dissension among brothers.
=20
        Proverbs 6:16-19
=20
http://dman.ddts.net/~dman/

--ADZbWkCsHQ7r3kzd
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj06KrgACgkQO8l8XBKTpRRMyACeKndJLXBOG3iIDOJm1vTjhl5p
irMAoKVZq88M93RwEowN+5euli5WF3dU
=+QjT
-----END PGP SIGNATURE-----

--ADZbWkCsHQ7r3kzd--