modulo

Python modulo on floats

梦想与她 提交于 2019-11-26 02:06:25
Can anyone explain how the modulo operator works in Python? I cannot understand why 3.5 % 0.1 = 0.1 . abarnert Actually, it's not true that 3.5 % 0.1 is 0.1 . You can test this very easily: >>> print(3.5 % 0.1) 0.1 >>> print(3.5 % 0.1 == 0.1) False In actuality, on most systems, 3.5 % 0.1 is 0.099999999999999811 . But, on some versions of Python, str(0.099999999999999811) is 0.1 : >>> 3.5 % 0.1 0.099999999999999811 >>> repr(3.5 % 0.1) '0.099999999999999811' >>> str(3.5 % 0.1) '0.1' Now, you're probably wondering why 3.5 % 0.1 is 0.099999999999999811 instead of 0.0 . That's because of the usual

Integer division with remainder in JavaScript?

邮差的信 提交于 2019-11-26 01:55:40
问题 In JavaScript, how do I get: the whole number of times a given integer goes into another? the remainder? 回答1: For some number y and some divisor x compute the quotient ( quotient ) and remainder ( remainder ) as: var quotient = Math.floor(y/x); var remainder = y % x; 回答2: I'm no expert in bitwise operators, but here's another way to get the whole number: var num = ~~(a / b); This will work properly for negative numbers as well, while Math.floor() will round in the wrong direction. This seems

How does java do modulus calculations with negative numbers?

青春壹個敷衍的年華 提交于 2019-11-26 01:49:27
问题 Am I doing modulus wrong? Because in Java -13 % 64 is supposed to evaluate to -13 but I get 51 . 回答1: Both definitions of modulus of negative numbers are in use - some languages use one definition and some the other. If you want to get a negative number for negative inputs then you can use this: int r = x % n; if (r > 0 && x < 0) { r -= n; } Likewise if you were using a language that returns a negative number on a negative input and you would prefer positive: int r = x % n; if (r < 0) { r +=

How does a hash table work?

故事扮演 提交于 2019-11-26 01:25:38
问题 I\'m looking for an explanation of how a hash table works - in plain English for a simpleton like me! For example, I know it takes the key, calculates the hash (I am looking for an explanation how) and then performs some kind of modulo to work out where it lies in the array where the value is stored, but that\'s where my knowledge stops. Could anyone clarify the process? Edit: I\'m not asking specifically about how hash codes are calculated, but a general overview of how a hash table works.

How does java do modulus calculations with negative numbers?

一个人想着一个人 提交于 2019-11-26 01:24:59
Am I doing modulus wrong? Because in Java -13 % 64 is supposed to evaluate to -13 but I get 51 . Both definitions of modulus of negative numbers are in use - some languages use one definition and some the other. If you want to get a negative number for negative inputs then you can use this: int r = x % n; if (r > 0 && x < 0) { r -= n; } Likewise if you were using a language that returns a negative number on a negative input and you would prefer positive: int r = x % n; if (r < 0) { r += n; } Since "mathematically" both are correct: -13 % 64 = -13 (on modulus 64) -13 % 64 = 51 (on modulus 64)

Python modulo on floats

你。 提交于 2019-11-26 01:01:44
问题 Can anyone explain how the modulo operator works in Python? I cannot understand why 3.5 % 0.1 = 0.1 . 回答1: Actually, it's not true that 3.5 % 0.1 is 0.1 . You can test this very easily: >>> print(3.5 % 0.1) 0.1 >>> print(3.5 % 0.1 == 0.1) False In actuality, on most systems, 3.5 % 0.1 is 0.099999999999999811 . But, on some versions of Python, str(0.099999999999999811) is 0.1 : >>> 3.5 % 0.1 0.099999999999999811 >>> repr(3.5 % 0.1) '0.099999999999999811' >>> str(3.5 % 0.1) '0.1' Now, you're

What&#39;s the syntax for mod in java

那年仲夏 提交于 2019-11-26 00:59:31
As an example in pseudocode: if ((a mod 2) == 0) { isEven = true; } else { isEven = false; } Cody Hatch For non-negative integers, you can use the remainder operator % . For your exact example: if ((a % 2) == 0) { isEven = true; } else { isEven = false; } This can be simplified to a one-liner: isEven = (a % 2) == 0; martinatime Here is the representation of your pseudo-code in minimal Java code; boolean isEven = a % 2 == 0; I'll now break it down into its components. The modulus operator in Java is the percent character (%). Therefore taking an int % int returns another int. The double equals

The modulo operation on negative numbers in Python

末鹿安然 提交于 2019-11-26 00:59:21
问题 I\'ve found some strange behaviour in Python regarding negative numbers: >>> -5 % 4 3 Could anyone explain what\'s going on? 回答1: Unlike C or C++, Python's modulo operator ( % ) always return a number having the same sign as the denominator (divisor). Your expression yields 3 because (-5) % 4 = (-2 × 4 + 3) % 4 = 3. It is chosen over the C behavior because a nonnegative result is often more useful. An example is to compute week days. If today is Tuesday (day #2), what is the week day N days

What&#39;s the syntax for mod in java

百般思念 提交于 2019-11-26 00:58:40
问题 As an example in pseudocode: if ((a mod 2) == 0) { isEven = true; } else { isEven = false; } 回答1: For non-negative integers, you can use the remainder operator % . For your exact example: if ((a % 2) == 0) { isEven = true; } else { isEven = false; } This can be simplified to a one-liner: isEven = (a % 2) == 0; 回答2: Here is the representation of your pseudo-code in minimal Java code; boolean isEven = a % 2 == 0; I'll now break it down into its components. The modulus operator in Java is the

JavaScript % (modulo) gives a negative result for negative numbers

左心房为你撑大大i 提交于 2019-11-26 00:15:38
问题 According to Google Calculator (-13) % 64 is 51 . According to Javascript (see this JSBin) it is -13 . How do I fix this? 回答1: Number.prototype.mod = function(n) { return ((this%n)+n)%n; }; Taken from this article: The JavaScript Modulo Bug 回答2: Using Number.prototype is SLOW, because each time you use the prototype method your number is wrapped in an Object . Instead of this: Number.prototype.mod = function(n) { return ((this % n) + n) % n; } Use: function mod(n, m) { return ((n % m) + m) %