MWorks integer (truncating) division

Hi Chris,

We just got bitten by a bug where we assumed division with integers to return an integral (truncated) result. I see that changed in MWorks 0.6 so the result of division is always floating point.

We will change to now use int casts after division when we want this.

However, any possibility of adding a // operator to provide truncating division as Python does?

thanks,
Mark

p.s. if you were using the python parser the behavior of / and // would already be well-specified. :slight_smile:

Hi Mark,

any possibility of adding a // operator to provide truncating division as Python does?

We certainly could add it, although I have my doubts about how useful it would be. I’m not sure that I’ve ever used Python’s // operator, and I’ve written an awful lot of Python code. Do you use it frequently?

Chris

Hi Chris,

I’ve found the // operator useful in two main cases. First, when porting python 2 to python 3 code (when the division semantics changed as in MWorks 0.6: the / operator in Py2 returns an integer result for two integer operands but in Py3 it returns a float). And second when working with objects of known integer types - useful e.g. for dividing numpy objects of known integer dtype.

Here I have a multi-byte number that I want to send across an 8-bit channel from within MWorks XML. I’d probably use a shift operator if present but it now works, just with a lot of int casts around the division. Not a big deal either way.

Mark