IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Description of Objects in VPython

Floating Division

By default, Python performs integer division with truncation, so that 3/4 is 0, not 0.75. This is inconvenient when doing scientific computations, and can lead to hard-to-find bugs in programs. You can write 3./4., which is 0.75 by the rules of "floating-point" division.

Starting with Python 2.2, you can change the default so that 3/4 is treated as 0.75. Place this at the start of your program:


from __future__ import division

 

There are two underscores ("_" and "_") before "future" and two after.

The Visual module converts integers to floating-point numbers for you when specifying attributes of objects:

    object.pos = (1,2,3) is equivalent to object.pos = (1.,2.,3.)

A related issue in versions of Python preceding Python 2.2 is that raising an integer to a negative power, as in 10**-2, gives an error. Instead, use 10. (a floating-point number) and write the expression as 10.**-2 in order to obtain the desired result (0.01 in this case). This is not a problem in Python 2.2 and later versions.