C++ operator % guarantees

In addition to Luchian‘s answer, this is the corresponding part from the C++11 standard: The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined. For integral … Read more

Fast division/mod by 10ˣ

Short Answer: NO Long Answer: NO. Explanation: The compiler is already optimizing statements like this for you. If there is a technique for implementing this quicker than an integer division then the compiler already knows about it and will apply it (assuming you turn on optimizations). If you provide the appropriate architecture flags as well … Read more

Using an iterator to Divide an Array into Parts with Unequal Size

The segfault you are seeing is coming from next checking the range for you is an assertion in your Debug implementation to check against undefined behavior. The behavior of iterators and pointers is not defined beyond the their allocated range, and the “one past-the-end” element: Are iterators past the “one past-the-end” iterator undefined behavior? This … Read more

C# ModInverse Function

Since .Net 4.0+ implements BigInteger with a special modular arithmetics function ModPow (which produces “X power Y modulo Z”), you don’t need a third-party library to emulate ModInverse. If n is a prime, all you need to do is to compute: a_inverse = BigInteger.ModPow(a, n – 2, n) For more details, look in Wikipedia: Modular … Read more

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

Edit Apr 19, 2013: 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 … Read more