modulo

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

こ雲淡風輕ζ 提交于 2019-11-27 09:50:30
I have an array which I need to divide up into 3-element sub-arrays. I wanted to do this with iterators, but I end up iterating past the end of the array and segfaulting even though I don't dereference the iterator . given: auto foo = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; I'm doing: auto bar = cbegin(foo); for (auto it = next(bar, 3); it < foo.end(); bar = it, it = next(bar, 3)) { for_each(bar, it, [](const auto& i) { cout << i << endl; }); } for_each(bar, cend(foo), [](const auto& i) { cout << i << endl; }); Now I can solve this by defining a finish iterator: auto bar = cbegin(foo); auto finish

Why does 2 mod 4 = 2?

你。 提交于 2019-11-27 09:14:48
问题 I'm embarrassed to ask such a simple question. My term does not start for two more weeks so I can't ask a professor, and the suspense would kill me. Why does 2 mod 4 = 2? 回答1: Mod just means you take the remainder after performing the division. Since 4 goes into 2 zero times, you end up with a remainder of 2. 回答2: Modulo is the remainder, not division. 2 / 4 = 0R2 2 % 4 = 2 The sign % is often used for the modulo operator, in lieu of the word mod . For x % 4 , you get the following table (for

How does Python implement the modulo operation?

这一生的挚爱 提交于 2019-11-27 07:48:18
问题 I'm curious in regards to the time and space complexities of the % operator in Python. Also, does Python use a bitwise operation for % 2 ? Edit: I'm asking about Python 2.7's implementation, just in case it differs slightly from that of Python 3 回答1: Python uses the classic Algorithm D from Knuth's 'The Art of Computer Programming'. The running time is (generally) proportional to the product of lengths of the two numbers. Space is proportional to the sum of the lengths of the two numbers. The

Check if a number is divisible by 3

 ̄綄美尐妖づ 提交于 2019-11-27 06:48:43
I need to find whether a number is divisible by 3 without using % , / or * . The hint given was to use atoi() function. Any idea how to do it? Subtract 3 until you either a) hit 0 - number was divisible by 3 b) get a number less than 0 - number wasn't divisible -- edited version to fix noted problems while n > 0: n -= 3 while n < 0: n += 3 return n == 0 MSalters The current answers all focus on decimal digits, when applying the "add all digits and see if that divides by 3". That trick actually works in hex as well; e.g. 0x12 can be divided by 3 because 0x1 + 0x2 = 0x3. And "converting" to hex

Fastest way to calculate a 128-bit integer modulo a 64-bit integer

社会主义新天地 提交于 2019-11-27 06:39:54
I have a 128-bit unsigned integer A and a 64-bit unsigned integer B. What's the fastest way to calculate A % B - that is the (64-bit) remainder from dividing A by B? I'm looking to do this in either C or assembly language, but I need to target the 32-bit x86 platform. This unfortunately means that I cannot take advantage of compiler support for 128-bit integers, nor of the x64 architecture's ability to perform the required operation in a single instruction. Edit: Thank you for the answers so far. However, it appears to me that the suggested algorithms would be quite slow - wouldn't the fastest

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

你。 提交于 2019-11-27 06:25:24
I'm looking for some nice C code that will accomplish effectively: while (deltaPhase >= M_PI) deltaPhase -= M_TWOPI; while (deltaPhase < -M_PI) deltaPhase += M_TWOPI; What are my options? 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

Weird Objective-C Mod Behavior for Negative Numbers

♀尐吖头ヾ 提交于 2019-11-27 05:50:51
问题 So I thought that negative numbers, when mod'ed should be put into positive space... I cant get this to happen in objective-c I expect this: -1 % 3 = 2 0 % 3 = 0 1 % 3 = 1 2 % 3 = 2 But get this -1 % 3 = -1 0 % 3 = 0 1 % 3 = 1 2 % 3 = 2 Why is this and is there a workaround? 回答1: result = n % 3; if( result < 0 ) result += 3; Don't perform extra mod operations as suggested in the other answers. They are very expensive and unnecessary. 回答2: In C and Objective-C, the division and modulus

Integer division & modulo operation with negative operands in Python

无人久伴 提交于 2019-11-27 03:58:27
问题 Questions arise when I type in these expressions to Python 3.3.0 -10 // 3 # -4 -10 % 3 # 2 10 // -3 # -4 10 % -3 # -2 -10 // -3 # 3 It appears as though it takes the approximate floating point (-3.33)? and rounds down either way in integer division but in the modulo operation it does something totally different. It seems like it returns the remainder +/-1 and only switches the sign depending on where the negative operand is. I am utterly confused, even after looking over other answers on this

operator modulo change in c++ 11? [duplicate]

你离开我真会死。 提交于 2019-11-27 03:21:29
问题 Possible Duplicate: C++ operator % guarantees In c++ 98/03 5.6-4 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; otherwise (a/b)*b + a%b is equal to a. If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined . In c++ 11: 5.6 -4 The binary / operator yields

Is there any alternative to using % (modulus) in C/C++?

拥有回忆 提交于 2019-11-27 03:03:47
I read somewhere once that the modulus operator is inefficient on small embedded devices like 8 bit micro-controllers that do not have integer division instruction. Perhaps someone can confirm this but I thought the difference is 5-10 time slower than with an integer division operation. Is there another way to do this other than keeping a counter variable and manually overflowing to 0 at the mod point? const int FIZZ = 6; for(int x = 0; x < MAXCOUNT; x++) { if(!(x % FIZZ)) print("Fizz\n"); // slow on some systems } vs: The way I am currently doing it: const int FIZZ = 6; int fizzcount = 1; for