compute the remainder of truncating division. If it's negative add 2*PI
double fmod2p(double arg) { double modu = arg.remainder(TWOPI); if (modu < 0.0) { modu += TWOPI; } return modu; }