modulo

C++ operator % guarantees

这一生的挚爱 提交于 2019-11-26 17:22:43
问题 Is it guaranteed that (-x) % m , where x and m are positive in c++ standard (c++0x) is negative and equals to -(x % m) ? I know it's right on all machines I know. 回答1: 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 operands the

How to sort Integer digits in ascending order without Strings or Arrays?

怎甘沉沦 提交于 2019-11-26 16:54:34
问题 I'm trying to sort the digits of an integer of any length in ascending order without using Strings, arrays or recursion. Example: Input: 451467 Output: 144567 I have already figured out how to get each digit of the integer with modulus division: int number = 4214; while (number > 0) { IO.println(number % 10); number = number / 10; } but I don't know how to order the digits without an array. Don't worry about the IO class; it's a custom class our professor gave us. 回答1: There's actually a very

Does either ANSI C or ISO C specify what -5 % 10 should be?

那年仲夏 提交于 2019-11-26 16:34:42
I seem to remember that ANSI C didn't specify what value should be returned when either operand of a modulo operator is negative (just that it should be consistent). Did it get specified later, or was it always specified and I am remembering incorrectly? C89, not totally (§3.3.5/6). It can be either -5 or 5, because -5 / 10 can return 0 or -1 ( % is defined in terms of a linear equation involving / , * and + ): When integers are divided and the division is inexact, if both operands are positive the result of the / operator is the largest integer less than the algebraic quotient and the result

Why does C++ output negative numbers when using modulo?

时间秒杀一切 提交于 2019-11-26 16:31:56
Math : If you have an equation like this: x = 3 mod 7 x could be ... -4, 3, 10, 17, ..., or more generally: x = 3 + k * 7 where k can be any integer. I don't know of a modulo operation is defined for math, but the factor ring certainly is. Python : In Python, you will always get non-negative values when you use % with a positive m : #!/usr/bin/python # -*- coding: utf-8 -*- m = 7 for i in xrange(-8, 10 + 1): print(i % 7) Results in: 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 C++: #include <iostream> using namespace std; int main(){ int m = 7; for(int i=-8; i <= 10; i++) { cout << (i % m) << endl; }

What does the percent sign mean in PHP?

跟風遠走 提交于 2019-11-26 16:20:52
问题 What exactly does this mean? $number = ( 3 - 2 + 7 ) % 7; 回答1: It's the modulus operator, as mentioned, which returns the remainder of a division operation. Examples: 3%5 returns 3, as 3 divided by 5 is 0 with a remainder of 3. 5 % 10 returns 5, for the same reason, 10 goes into 5 zero times with a remainder of 5. 10 % 5 returns 0, as 10 divided by 5 goes exactly 2 times with no remainder. In the example you posted, (3 - 2 + 7) works out to 8, giving you 8 % 7 , so $number will be 1 , which

Is & faster than % when checking for odd numbers?

时间秒杀一切 提交于 2019-11-26 15:59:09
问题 To check for odd and even integer, is the lowest bit checking more efficient than using the modulo? >>> def isodd(num): return num & 1 and True or False >>> isodd(10) False >>> isodd(9) True 回答1: Yep. The timeit module in the standard library is how you check on those things. E.g: AmAir:stko aleax$ python -mtimeit -s'def isodd(x): x & 1' 'isodd(9)' 1000000 loops, best of 3: 0.446 usec per loop AmAir:stko aleax$ python -mtimeit -s'def isodd(x): x & 1' 'isodd(10)' 1000000 loops, best of 3: 0

Best way to make Java&#39;s modulus behave like it should with negative numbers?

我的未来我决定 提交于 2019-11-26 15:05:42
In java when you do a % b If a is negative, it will return a negative result, instead of wrapping around to b like it should. What's the best way to fix this? Only way I can think is a < 0 ? b + a : a % b Peter Lawrey It behaves as it should a % b = a - a / b * b; i.e. it's the remainder. You can do (a % b + b) % b This expression works as the result of (a % b) is necessarily lower than b , no matter if a is positive or negative. Adding b takes care of the negative values of a , since (a % b) is a negative value between -b and 0 , (a % b + b) is necessarily lower than b and positive. The last

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

倖福魔咒の 提交于 2019-11-26 14:55:31
问题 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 <<

Why is the behavior of the modulo operator (%) different between C and Ruby for negative integers?

不想你离开。 提交于 2019-11-26 14:42:26
问题 I was running some code in here. I tried -40 % 3 . It gives me the output 2 . when I performed the same operation in C, I get: int i = (-40) % 3 printf("%d", i); output is -1 How are both languages performing the modulo operation internally? 回答1: Wiki says: Given two positive numbers , a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n . .... When either a or n is negative, the naive definition breaks down and

How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers

谁说胖子不能爱 提交于 2019-11-26 14:24:51
One of my pet hates of C-derived languages (as a mathematician) is that (-1) % 8 // comes out as -1, and not 7 fmodf(-1,8) // fails similarly What's the best solution? C++ allows the possibility of templates and operator overloading, but both of these are murky waters for me. examples gratefully received. Armen Tsirunyan First of all I'd like to note that you cannot even rely on the fact that (-1) % 8 == -1 . the only thing you can rely on is that (x / y) * y + ( x % y) == x . However whether or not the remainder is negative is implementation-defined . Now why use templates here? An overload