modulo

Rounding time in Python

≡放荡痞女 提交于 2019-11-30 01:37:23
What would be an elegant, efficient and Pythonic way to perform a h/m/s rounding operation on time related types in Python with control over the rounding resolution? My guess is that it would require a time modulo operation. Illustrative examples: 20:11:13 % (10 seconds) => (3 seconds) 20:11:13 % (10 minutes) => (1 minutes and 13 seconds) Relevant time related types I can think of: datetime.datetime \ datetime.time struct_time Le Droid For a datetime.datetime rounding, see this function: https://stackoverflow.com/a/10854034/1431079 Sample of use: print roundTime(datetime.datetime(2012,12,31,23

Modulus division when first number is smaller than second number

僤鯓⒐⒋嵵緔 提交于 2019-11-30 01:14:04
问题 I apologize if this is a simple question but I'm having trouble grasping the concept of modulus division when the first number is smaller than the second number. For example when 1 % 4 my book says the remainder is 1. I don't understand how 1 is the remainder of 1 % 4. 1 / 4 is 0.25. Am I thinking about modulus division incorrectly? 回答1: First, in Java, % is the remainder (not modulo) operator, which has slightly different semantics. That said, you need to think in terms of integer-only

How does a modulo operation work when the first number is smaller?

淺唱寂寞╮ 提交于 2019-11-29 21:18:56
I'm messing with the modulo operation in python and I understand that it will spit back what the remainder is. But what if the first number is smaller than the second? for instance 2 % 5 the answer is 2. How does that work? 2/5 = .4 Does this help 22 % 5 = 2 17 % 5 = 2 12 % 5 = 2 7 % 5 = 2 2 % 5 = 2 Maybe this 22 / 5 = 4 + 2/5 17 / 5 = 3 + 2/5 12 / 5 = 2 + 2/5 7 / 5 = 1 + 2/5 2 / 5 = 0 + 2/5 five goes into 2 zero times. 5*0 = 0 2-0 = 2. the answer is 2 2 divided by 5 (integer division) is 0 with a remainder of 2. 2 = 0 x 5 + 2 for instance 2 % 5 the answer is 2. How does that work? 2/5 = .4!

What are the practical uses of modulus (%) in programming? [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-11-29 20:31:24
Possible Duplicate: Recognizing when to use the mod operator What are the practical uses of modulus? I know what modulo division is. The first scenario which comes to my mind is to use it to find odd and even numbers, and clock arithmetic. But where else I could use it? The most common use I've found is for "wrapping round" your array indices. For example, if you just want to cycle through an array repeatedly, you could use: int a[10]; for (int i = 0; true; i = (i + 1) % 10) { // ... use a[i] ... } The modulo ensures that i stays in the [0, 10) range. Edmund I usually use them in tight loops,

remquo Results Not Making Sense

不问归期 提交于 2019-11-29 17:36:09
I read here that remquo should return: If successful, returns the floating-point remainder of the division x/y as defined in std::remainder, and a value whose sign is the sign of x/y and whose magnitude is congruent modulo 2 n to the magnitude of the integral quotient of x/y , where n is an implementation-defined integer greater than or equal to 3 . Now clearly I've misunderstood all that techno-speak cause I thought that I was going to get back the fractional result of the division. Instead this operation: int quot; const float rem = remquo(7.0F, 4.0F, &quot); Sets quot to 2 and rem to -1.0 !

How can I Initialize a div_t Object?

不羁的心 提交于 2019-11-29 15:23:13
So the order of the members returned from the div functions seems to be implementation defined. Is quot the 1 st member or is rem ? Let's say that I'm doing something like this: generate(begin(digits), end(digits), [i = div_t{ quot, 0 }]() mutable { i = div(i.quot, 10); return i.rem; }) Of course the problem here is that I don't know if I initialized i.quot or i.rem in my lambda capture. Is intializing i with div(quot, 1) the only cross platform way to do this? EDIT: I think the VS workaround could look like this: #include <cstdlib> #include <type_traits> template<class T> struct DTMaker {

What determines the sign of m % n for integers?

会有一股神秘感。 提交于 2019-11-29 13:46:48
The modulo in Python is confusing. In Python, % operator is calculating the remainder: >>> 9 % 5 4 However: >>> -9 % 5 1 Why is the result 1 ? and not -4 ? Because in python, the sign matches the denominator. >>> 9 % -5 -1 >>> -9 % 5 1 For an explanation of why it was implemented this way, read the blog post by Guido . -10 % 5 is 0, ie, -10 is evenly divided by 5. You ask why -9 % 5 is not -4, and the answer is that both 1 and -4 can be correct answers, it depends on what -9 divided by 5 is. Of course -9 divided by 5 is 1.8, but this is integer division, in Python 3 represented by //, so I'll

Modulo operator in Objective-C returns the wrong result

自古美人都是妖i 提交于 2019-11-29 13:28:06
I'm a little freaked out by the results I'm getting when I do modulo arithmetic in Objective-C. -1 % 3 is coming out to be -1, which isn't the right answer: according to my understanding, it should be 2. -2 % 3 is coming out to -2, which also isn't right: it should be 1. Is there another method I should be using besides the % operator to get the correct result? Isaac Objective-C is a superset of C99 and C99 defines a % b to be negative when a is negative. See also the Wikipedia entry on the Modulo operation and this StackOverflow question . Something like (a >= 0) ? (a % b) : ((a % b) + b)

How can I calculate (A*B)%C for A,B,C <= 10^18, in C++?

喜你入骨 提交于 2019-11-29 11:08:29
For example, A=10^17, B=10^17, C=10^18. The product A*B exceeds the limit of long long int. Also, writing ((A%C)*(B%C))%C doesn't help. Assuming you want to stay within 64-bit integer operations, you can use binary long division, which boils down to a bunch of adds and multiply by two operations. This means you also need overflow-proof versions of those operators, but those are relatively simple. Here is some Java code that assumes A and B are already positive and less than M. If not, it's easy to make them so beforehand. // assumes a and b are already less than m public static long addMod

'MOD' is not a recognized built-in function name

ぐ巨炮叔叔 提交于 2019-11-29 10:46:34
问题 I wanted to use MOD function in SQL Server 2008R2 and followed this link but still got the message: 'MOD' is not a recognized built-in function name. DECLARE @m INT SET @m = MOD(321,11) SELECT @m Error: Msg 195, Level 15, State 10, Line 2 'MOD' is not a recognized built-in function name. Why I can't use this function from the link above? 回答1: The MOD keyword only exists in the DAX language (tabular dimensional queries), not TSQL Use % instead. Ref: Modulo 回答2: In TSQL, the modulo is done with