modulo

Modulus/Remainder function for non-integers

感情迁移 提交于 2019-11-29 09:11:38
rem gives this: Prelude> rem 9 8 1 I wanted something like this: Prelude> nonIntRem 9.1 8 1.0999999999999996 I implemented it like this: nonIntRem x y = x - (y * (fromIntegral $ truncate (x/y))) My questions are: Does something like this already exist in a standard Haskell library? I'd prefer to use a standard function, and I may have missed it. If not, is there a more standard name for this function in other languages? Maybe fmod, but the behavior for negatives in this case is not like mod, but like rem. If there is no standard name, can you think of a better name for this function? It seems

Why is modulus different in different programming languages?

怎甘沉沦 提交于 2019-11-29 09:06:55
Perl print 2 % -18; --> -16 Tcl puts [expr {2 % -18}] --> -16 but VBScript wscript.echo 2 mod -18 --> 2 Why the difference? The wikipedia answer is fairly helpful here. A short summary is that any integer can be defined as a = qn + r where all of these letters are integers, and 0 <= |r| < |n|. Almost every programming language will require that (a/n) * n + (a%n) = a. So the definition of modulus will nearly always depend on the definition of integer division. There are two choices for integer division by negative numbers 2/-18 = 0 or 2/-18 = -1. Depending on which one is true for your language

Other ways of performing modulo operation

南楼画角 提交于 2019-11-29 07:32:12
Some time ago I've seen somewhere a trick to perform modulo operation using bit operators. But now I cannot in any way perform proper operation. Anyone knows how to do it ? From what I remember it was faster than using %. The "trick" is to binary AND a value with 1. Any odd number must have the first bit set to 1. So var foo = 7; if( foo & 1 ) { // true } Using a bitwise AND has a better performance in almost all platforms / browsers. for(var loop = 0; loop < 10; loop++) { if( loop & 1 ) { console.log('I am ', loop, ' and I am odd!'); } } You can do the modulo of 2^k (a power of 2) by ANDing

random over a range, is number bias present for new rand() version?

筅森魡賤 提交于 2019-11-29 05:18:58
reading from various other SO questions, when using rand() % N you may happen to modify the bias for the pseudo number you get, so you usually have to introduce some range handling. However in all cases rand() was always mentioned, and not the newer random() or arcrandom4() functions or the native C++11 methods. What happens when you run these routines over a set? Do you get a bias like rand()? Thanks. What happens when you run these routines over a set? Do you get a bias like rand()? The answer is: this depends on the relation between the size of range returned by the generator and the

nth-child with mod (or modulo) operator

微笑、不失礼 提交于 2019-11-29 03:18:45
Is it possible to use the nth-child with modulo? I know you can specify a formula, such as nth-child(4n+2) But I can't seem to find if there's a modulo operator. I've tried the following examples below, and none seem to work: nth-child(n%7) nth-child(n % 7) nth-child(n mod 7) No, :nth-child() only supports addition, subtraction and coefficient multiplication . I gather you're trying to pick up the first 6 elements (as n mod 7 for any positive integer n only gives you 0 to 6 ). For that, you can use this formula instead: :nth-child(-n+6) By negating n , element counting is done backwards

Why is modulus operator slow?

我怕爱的太早我们不能终老 提交于 2019-11-29 03:02:20
问题 Paraphrasing from in "Programming Pearls" book (about c language on older machines, since book is from the late 90's): Integer arithmetic operations ( + , - , * ) can take around 10 nano seconds whereas the % operator takes up to 100 nano seconds. Why there is that much difference? How does a modulus operator work internally? Is it same as division ( / ) in terms of time? 回答1: The modulus/modulo operation is usually understood as the integer equivalent of the remainder operation - a side

Efficient (cycles wise) algorithm to compute modulo 25?

﹥>﹥吖頭↗ 提交于 2019-11-29 01:36:14
问题 I have a code in which i am computing x % 25. x always takes a positive value but its dynamic range is large. I found out that this particular code piece of computing a x % 25 is taking large cycles. I need to optimize it. Pre-computed lookup table is ruled out due to the possible large memory size of the table. As second approach i coded a fragment below(C code) - mod(a, b) { int r = a; while(r >= b) { r = r - b; } return r; } 1.) How can i optimize this code further for cycles(squeeze it to

Modulo for negative dividends in Python

旧时模样 提交于 2019-11-29 00:15:29
Been looking through other answers and I still don't understand the modulo for negative numbers in python For example the answer by df x == (x/y)*y + (x%y) so it makes sense that (-2)%5 = -2 - (-2/5)*5 = 3 Doesn't this (-2 - (-2/5)*5) =0 or am I just crazy? Modulus operation with negatives values - weird thing? Same with this negative numbers modulo in python Where did he get -2 from? Lastly if the sign is dependent on the dividend why don't negative dividends have the same output as their positive counterparts? For instance the output of print([8%5,-8%5,4%5,-4%5]) is [3, 2, 4, 1] In Python,

Divide and Get Remainder at the same time?

久未见 提交于 2019-11-28 22:21:19
Apparently, x86 (and probably a lot of other instruction sets) put both the quotient and the remainder of a divide operation in separate registers. Now, we can probably trust compilers to optimize a code such as this to use only one call to divide: ( x / 6 ) ( x % 6 ) And they probably do. Still, do any languages (or libraries, but mainly looking for languages) support giving both the divide and modulo results at the same time? If so, what are they, and What does the syntax look like? C has div and ldiv . Whether these generate separate instructions for the quotient and remainder will depend

Is there an expression using modulo to do backwards wrap-around (“reverse overflow”)?

流过昼夜 提交于 2019-11-28 18:49:24
For any whole number input W restricted by the range R = [ x , y ], the "overflow," for lack of a better term, of W over R is W % (y-x+1) + x . This causes it wrap back around if W exceeds y . As an example of this principle, suppose we iterate over a calendar's months: int this_month = 5; int next_month = (this_month + 1) % 12; where both integers will be between 0 and 11, inclusive. Thus, the expression above "clamps" the integer to the range R = [0,11]. This approach of using an expression is simple, elegant, and advantageous as it omits branching . Now, what if we want to do the same thing