Python and integer division

September 21st, 2006 by Patrick Boucher - Viewed 2632 times - Popularity: 6%




Not necessarily XSI stuff per say but, for anyone doing Python inside of XSI, especially if your starting out, you should be careful of a little gotcha called integer division.

Try this out in your script editor (Those of you who use the Python shell for quick tests can come up with the code for that environment rather easily):

1
2
Application.LogMessage(str(1/2))
Application.LogMessage(str(1/2.0))

Were you expecting 0.5 for both lines of code? GOTCHA! The first one returns 0 which is the integer division of 1/2.0

When dividing by an integer, Python returns an integer. If the result should have been fractional, the language will convert to int before handing it to you. Pain in the ass, I know, and I sometimes get bitten by it. I bitch and whine every time.

If you want to fix things more or less permanently add this piece of code to the top of your scripts and plugins:

1
from __future__ import division

The __future__ module is a module that implements things that will become standard in some ulterior version of Python. In this case your switching the behavior of the division operator (/) to what you would expect. If you do want to use integer division you’ll then have to use // instead of /.

Cheers

2 Responses to “Python and integer division”

  1. Helge Mathee Says:

    Those kind of things always get me now and then. It is the same for C++ by the way.

  2. Randolph Says:

    Chroseus

    Neither here nor there…

Leave a Reply