Unless I’m missing something, it seems the “mod” function is returning bogus results.
Discovered this when writing a converter for HSV to RGB. Didn’t work with the built in “mod” funciton. I wrote a custom “mod” function for the script and everything was OK.
Hope this helps.