Dividing within functions

Here’s an interesting one…Using python script.

I have code that references another module. Whenever I perform division in a function that is local to the current module it returns the result (float) perfectly, however, if I perform the exact same division in a function that is inside another module it does not. Everything I have read with Python states that if you use the / operator, it will return a float. If you use the // operator it will return an int.

See for yourself.
In a script copy the following code and adjust where the path for your scripts. You will need to rename the CommonScript reference to another script file name that you will also create.

import sys

sys.path.append(r'C:\Python Scripts')
from CommonScripts import *

def TestMeInThisScript(c):
    print 'Calling from this script.'
    print 'Passed value: ' + str(c)
    print 'Calced value: ' + str(c / 3)

if __name__ == '__main__':

In another script file (mine was called CommonScript) copy the following code:

def TestMeInOtherScript(c):
    print 'Calling from other script.'
    print 'Passed value: ' + str(c)
    print 'Calced value: ' + str(c / 3)

The results I receive are:

Calling from this script.
Passed value: 2
Calced value: 0.666666666667
Calling from other script.
Passed value: 2
Calced value: 0

Why would it matter if the operation was done in the local script vs an external script?

Hi Mike - feed the script 2.0 so that it knows the number is a float - or specify a float - otherwise it seems to treat it as an int.


Hi Pascal - Thanks. It’s just odd that it works just fine inside the same file but when referencing the external file it does not. Sounds like a bug to me. It should work one way or the other in both locations. As a work around I just added the following prior to the calc:

c = float(c)

If you run it in the 2.7.18 Python shell, both functions return 0.
If you run it in the 3.7.8 Python shell, both functions return 0.6666666666666666

Ah ok that explains it’s an underlying Python issue. I know Rhino uses IronPython which is stuck on 2.7. It sounds like 3.8 is the latest for Python and 3.4.0 beta for IronPython. Anyone know if Rhino 8 will move away from IronPython or implement IP 3.4.0 if it’s out of beta in time? Just curious.

See Rhino 8 Feature: ScriptEditor (CPython, CSharp)

Hi Mike,

Not sure why you get different results on the main and inported methods.

But to avoid this all together there is a provision to import the future division.

I have all my scripts for Rhino starting like so:

# coding=utf-8
from __future__ import division

import rhinoscriptsyntax as rs


Wow…that’s great! Can’t wait for version 8 in general.

This is great to know! Thanks!