modulo

How to make a modulo operation in objective-c / cocoa touch?

☆樱花仙子☆ 提交于 2019-11-26 08:07:48
问题 I have two CGFloat values, and want to calculate the modulo result. Or in other words: I want to know what\'s left if valueA is placed as much as possible into valueB. So I just tried: CGFloat moduloResult = valueB % valueA; the compiler complains about the % and tells me: \"invalid operands to binary %\". Any idea? 回答1: % is for int or long , not float or double . You can use fmod() or fmodf() from <math.h> instead. Better is <tgmath.h> as suggested by the inventor of CGFloat. 回答2: If I

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

杀马特。学长 韩版系。学妹 提交于 2019-11-26 05:53:18
问题 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

Integer division with remainder in JavaScript?

懵懂的女人 提交于 2019-11-26 05:39:18
In JavaScript, how do I get: the whole number of times a given integer goes into another? the remainder? Mark Elliot 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; user113716 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 correct as well: var num = (a / b) >> 0; I did some speed tests on Firefox. -100/3 // -33

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

微笑、不失礼 提交于 2019-11-26 04:52:55
问题 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? 回答1: 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

C and Python - different behaviour of the modulo (%) operation

孤人 提交于 2019-11-26 04:38:36
问题 I have found that the same mod operation produces different results depending on what language is being used. In Python: -1 % 10 produces 9 In C it produces -1 ! Which one is the right modulo? How to make mod operation in C to be the same like in Python? 回答1: Both variants are correct, however in mathematics (number theory in particular), Python's modulo is most commonly used. In C, you do ((n % M) + M) % M to get the same result as in Python. E. g. ((-1 % 10) + 10) % 10 . Note, how it still

What does the percentage sign mean in Python

送分小仙女□ 提交于 2019-11-26 04:25:30
问题 In the tutorial there is an example for finding prime numbers: >>> for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print(n, \'equals\', x, \'*\', n//x) ... break ... else: ... # loop fell through without finding a factor ... print(n, \'is a prime number\') ... I understand that the double == is a test for equality, but I don\'t understand the if n % x part. Like I can verbally walk through each part and say what the statement does for the example. But I don\'t

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

拈花ヽ惹草 提交于 2019-11-26 03:54:43
问题 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. 回答1: 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

Can&#39;t Mod Zero?

谁说我不能喝 提交于 2019-11-26 03:36:54
问题 Why is X % 0 an invalid expression? I always thought X % 0 should equal X. Since you can\'t divide by zero, shouldn\'t the answer naturally be the remainder, X (everything left over)? 回答1: The C++ Standard(2003) says in §5.6/4, [...] If the second operand of / or % is zero the behavior is undefined ; [...] That is, following expressions invoke undefined-behavior(UB): X / 0; //UB X % 0; //UB Note also that -5 % 2 is NOT equal to -(5 % 2) (as Petar seems to suggest in his comment to his answer)

Mod of negative number is melting my brain

百般思念 提交于 2019-11-26 03:29:13
问题 I\'m trying to mod an integer to get an array position so that it will loop round. Doing i % arrayLength works fine for positive numbers but for negative numbers it all goes wrong. 4 % 3 == 1 3 % 3 == 0 2 % 3 == 2 1 % 3 == 1 0 % 3 == 0 -1 % 3 == -1 -2 % 3 == -2 -3 % 3 == 0 -4 % 3 == -1 so i need an implementation of int GetArrayIndex(int i, int arrayLength) such that GetArrayIndex( 4, 3) == 1 GetArrayIndex( 3, 3) == 0 GetArrayIndex( 2, 3) == 2 GetArrayIndex( 1, 3) == 1 GetArrayIndex( 0, 3) ==

Can&#39;t use modulus on doubles?

你说的曾经没有我的故事 提交于 2019-11-26 02:33:21
问题 I have a program in C++ (compiled using g++). I\'m trying to apply two doubles as operands to the modulus function, but I get the following error: error: invalid operands of types \'double\' and \'double\' to binary \'operator%\' Here\'s the code: int main() { double x = 6.3; double y = 2; double z = x % y; } 回答1: The % operator is for integers. You're looking for the fmod() function. #include <cmath> int main() { double x = 6.3; double y = 2.0; double z = std::fmod(x,y); } 回答2: fmod(x, y) is