How to wrap a float to the interval [-pi, pi)

Modulo function updated to handle boundary cases as noted by aka.nice and arr_sea: static const double _PI= 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348; static const double _TWO_PI= 6.2831853071795864769252867665590057683943387987502116419498891846156328125724179972560696; // Floating-point modulo // The result (the remainder) has same sign as the divisor. // Similar to matlab’s mod(); Not similar to fmod() – Mod(-3,4)= 1 fmod(-3,4)= -3 template<typename T> T Mod(T … Read more

Is divmod() faster than using the % and // operators?

To measure is to know (all timings on a Macbook Pro 2.8Ghz i7): >>> import sys, timeit >>> sys.version_info sys.version_info(major=2, minor=7, micro=12, releaselevel=”final”, serial=0) >>> timeit.timeit(‘divmod(n, d)’, ‘n, d = 42, 7’) 0.1473848819732666 >>> timeit.timeit(‘n // d, n % d’, ‘n, d = 42, 7’) 0.10324406623840332 The divmod() function is at a disadvantage here because … Read more