modulus

Faster modulus in C/C#?

徘徊边缘 提交于 2019-11-30 06:45:34
Is there a trick for creating a faster integer modulus than the standard % operator for particular bases? For my program, I'd be looking for around 1000-4000 (e.g. n%2048). Is there a quicker way to perform n modulus 2048 than simply: n%2048 ? If the denominator is known at compile time to be a power of 2, like your example of 2048, you could subtract 1 and do a bitwise-and. That is: n % m == n & (m - 1) ...where m is a power of 2. For example: 22 % 8 == 22 - 16 == 6 Dec Bin ----- ----- 22 = 10110 8 = 01000 8 - 1 = 00111 22 & (8 - 1) = 10110 & 00111 ------- 6 = 00110 Bear in mind that a good

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

旧城冷巷雨未停 提交于 2019-11-30 03:01:54
I remember from assembly that integer division instructions yield both the quotient and remainder. So, in python will the built-in divmod() function be better performance-wise than using the % and // operators (suppose of course one needs both the quotient and the remainder)? q, r = divmod(n, d) q, r = (n // d, n % d) 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,

What are the rules for modular arithmetic in C?

只谈情不闲聊 提交于 2019-11-30 00:29:22
问题 In earlier classes, I was taught that n % d = r and to think about it as n = d*q + r , where d is the divisor, q is the quotient, and r is the remainder (noting that the remainder can never be negative). So for instance, -111 mod 11 is 10 , because -111 = -11*-11 + 10 (as opposed to -111 = -11*10 -1 , seeing as how that would give us a negative remainder). However, when printing the results of -111 % 11 , -1 is the result and not 10 . Why? Isn't this technically wrong? 回答1: Short Answer: The

Mod with Doubles

爱⌒轻易说出口 提交于 2019-11-29 09:42:46
Am I doing something wrong or does the VBA Mod operator actually not work with floating point values like Doubles? So I've always sort of assumed that the VBA Mod operator would work with Doubles based on the VB documentation , but in trying to figure out why my rounding function doesn't work, I found some unexpected Mod behavior. Here is my code: Public Function RoundUp(num As Double, Optional nearest As Double = 1) RoundUp = ((num \ nearest) - ((num Mod nearest) > 0)) * nearest End Function RoundUp(12.34) returns 12 instead of 13 so I dug a little deeper and found that: 12.5 Mod 1 returns 0

Sorting with a modulus

萝らか妹 提交于 2019-11-29 08:03:48
I am trying trying to sort a list into columns with uksort. The array already alpha sorted, so it is like array('A','B','C','D','E','F','G','H','I','J','K','L','M') Which gets displayed in html, as floated elements: A B C D E F G H I J K L M I want it reordered so it displays like this: A E H K B F I L C G J M D So the sorted array would be: array('A','E','H','K','B','F','I','L','C','G','J','M','D' Basically, the same as Sorting a list alphabetically with a modulus but for php. I've tried taking the solution for javascript and convert it into php, but I'm not getting something right. Anyone

Why is operator% referred to as the “modulus” operator instead of the “remainder” operator?

此生再无相见时 提交于 2019-11-29 05:30:06
Today at work I had an interesting discussion with one of my coworkers. He was surprised when he had the following happen to him: assert(-1 % 10 == -1) //Expecting 9 So when he came to ask me about it, I told him "well, that makes sense. When you divide -1 by 10, you get 0 with -1 remaining. His argument however was that the modulus operator is supposed to hold true to the "always positive" model. I did a little research and found that the modulus he was referring to looks like this: Let q be the integer quotient of a and n. Let r be the remainder. Then: a = n * q + r The definition I was

Modulus gives wrong outcome?

核能气质少年 提交于 2019-11-29 01:34:48
Could anyone tell me why these two modulus calculations yield two different outcomes? I just need to blame someone or something but me for all those hours I lost finding this bug. public void test1() { int stepAmount = 100; float t = 0.02f; float remainder = t % (1f / stepAmount); Debug.Log("Remainder: " + remainder); // Remainder: 0.01 float fractions = 1f / stepAmount; remainder = t % fractions; Debug.Log("Remainder: " + remainder); // Remainder: 0 } Using VS-2017 V15.3.5 My best bet is that this is due to the liberty the runtime has to perform floating point operations with higher precision

Reverse Modulus Operator

蓝咒 提交于 2019-11-28 19:11:51
Over 3 years after asking the question I found the solution. I have included it as an answer . I have an expression with modulus in it that needs to be put in terms of x. (a + x) mod m = b I can't figure out what to do with the modulus. Is there a way to get x by itself, or am I out of luck on this one? Edit : I realize that I can get multiple answers, but I'm looking for an answer that falls within the range of m. I was revisiting this question and realized it is possible based off of the answer @Gorcha gave. (a + x) mod m = b a + x = nm + b x = nm + b - a for some integer n I don't know why

What does “% is unavailable: Use truncatingRemainder instead” mean?

余生颓废 提交于 2019-11-28 16:17:15
I get the following error when using code for an extension, I'm not sure if they're asking to just use a different operator or modify the values in the expression based on an internet search. Error: % is unavailable: Use truncatingRemainder instead Extension code: extension CMTime { var durationText:String { let totalSeconds = CMTimeGetSeconds(self) let hours:Int = Int(totalSeconds / 3600) let minutes:Int = Int(totalSeconds % 3600 / 60) let seconds:Int = Int(totalSeconds % 60) if hours > 0 { return String(format: "%i:%02i:%02i", hours, minutes, seconds) } else { return String(format: "%02i:

Divison and remainder in Prolog

[亡魂溺海] 提交于 2019-11-28 14:07:52
Trying to figure out how to write a recursive predicate divide_by(X, D, I, R) that takes as input a positive integer X and a divisor D, and returns the answer as the whole number part I and the remainder part R, however, I can't seem to get my head around Prolog. How would I go about doing this? false There are predefined evaluable functors for this. (div)/2 and (mod)/2 always rounding down. Recommended by LIA-1, Knuth etc. (//)/2 and (rem)/2 rounding toward zero (actually, it's implementation defined, but all current implementations do it like that). You can ask this via current_prolog_flag