FIXED: Modulo Operator not working as expected in Expression CHOP

Hello Peoples,
reporting about modulo operator (%) not working as expected in Expression CHOP.

Quick and easy to reproduce, constant CHOP with value -0.5, plugged into Expression CHOP with expression me.inputVal%1.
The result of the Expression is still -0.5, should be 0.5.

The same calculation (-0.5%1) done in the Texport & DATs Console or a Script CHOP give the correct result (0.5).

Modulo operator still works correctly on positive values.

Build version is : TouchDesigner.2023.12000
Thanks •ᴗ•

Modulo results on negative numbers are not universaly equals…
see here

To be sure to obtain a positive result, use abs() after modulo.

Yes depending on the language, negative value in Modulo can result in negative result, but not in Python.

The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand

From Official Python Documentation on Modulo Operator

Second operand is positive, result should be positive

This may be a bug with optimized expressions. I’ll take a look

Are those optimized expressions runned using C++ instead of Python for optimization reason ?
This would make sense, because the modulo act like a C++ Modulo instead of a Python one

Thanks •ᴗ•

Yep exactly. This will be fixed in builds 2023.12089 and later. Thanks for the report!

1 Like