Since Python's modulo operator % isn't available in Simple Expressions:
Blender can evaluate a useful subset of Python driver expressions directly, which significantly improves performance, especially on multi-core systems.
Since Python drivers are considerably slower how would one port a Python formula using % modulo operator, to use fmod function (or something else?) instead?
Below a testing script to compare % operator with fmod function:
from math import fmod
def fakemod(a, n):
return fmod(a, n)
row = "| {:>7} " * 5 + "|"
header = row.replace('>','^').replace('<','^')
line = header.replace(' ','-').replace('^','-^').replace('|','+').format('-'5)
print(line)
print(header.format(" ", " ", "fakemod", " ", "fakemod"))
print(header.format("i", "i % 4", "(i, 4)", "i % -4", "(i, -4)"))
print(line)
equal = lambda a,b: '==' if a == b else '!='
for i in range(-10, 10):
a, b, c, d = i % 4, fakemod(i, 4), i % -4, fakemod(i, -4)
print(row.format(i, a, f"{equal(a,b)} {b}", c, f"{equal(c,d)} {d}"))
print(line)
I'm looking for a way to remove the inconsistencies between % and fakemod. For readability I mark the inconsistencies with != below:
+---------+---------+---------+---------+---------+
| | | fakemod | | fakemod |
| i | i % 4 | (i, 4) | i % -4 | (i, -4) |
+---------+---------+---------+---------+---------+
| -10 | 2 | != -2.0 | -2 | == -2.0 |
| -9 | 3 | != -1.0 | -1 | == -1.0 |
| -8 | 0 | == -0.0 | 0 | == -0.0 |
| -7 | 1 | != -3.0 | -3 | == -3.0 |
| -6 | 2 | != -2.0 | -2 | == -2.0 |
| -5 | 3 | != -1.0 | -1 | == -1.0 |
| -4 | 0 | == -0.0 | 0 | == -0.0 |
| -3 | 1 | != -3.0 | -3 | == -3.0 |
| -2 | 2 | != -2.0 | -2 | == -2.0 |
| -1 | 3 | != -1.0 | -1 | == -1.0 |
| 0 | 0 | == 0.0 | 0 | == 0.0 |
| 1 | 1 | == 1.0 | -3 | != 1.0 |
| 2 | 2 | == 2.0 | -2 | != 2.0 |
| 3 | 3 | == 3.0 | -1 | != 3.0 |
| 4 | 0 | == 0.0 | 0 | == 0.0 |
| 5 | 1 | == 1.0 | -3 | != 1.0 |
| 6 | 2 | == 2.0 | -2 | != 2.0 |
| 7 | 3 | == 3.0 | -1 | != 3.0 |
| 8 | 0 | == 0.0 | 0 | == 0.0 |
| 9 | 1 | == 1.0 | -3 | != 1.0 |
+---------+---------+---------+---------+---------+