I am not very sure regarding the use case but you can try this function. It seems to work as a very good approximation and everything seems differentiable. I have just made some back of the envelope calculations regarding how gradient might work in such a situation. Hope you find it useful.
fmod( x, a ) ≈ ( a / π ) arctan( tan( π ( x / a - 0.5 ) ) ) + a / 2
link to a small demonstration of the function: https://www.desmos.com/calculator/havtieuhey 16
image1366×460 66.9 KB