Sorry for a possible repeat question the % symbol doesn't coincide with searchability.
What does % mean? I can't seem to stick this one down.
Example:
rotation = value % MathHelper.TwoPi;
is a specific instance.
But I have found code that uses % more often. Modulus I 'think' it is called, but I am not positive.
Previous Post:
With well thought out awnser
% Operator (C# Reference)
The % operator computes the remainder after dividing its first operand
by its second. All numeric types have predefined remainder operators.
See MSDN:
C# Operators
% Operator (C# Reference)
It is the modulus operator. It computes the remainder after dividing its first operand by its second, e.g.
5 % 2 = 1
6 % 2 = 0
5 % 3 = 2.
in C# it means modulus, which is basically a remainder of
example:
int remainder = 10 % 3 //remainder is 1
It is the modulus operator. It returns the remainder of an integer.
int remainder = 2 % 1; // (remainder variable is assigned to 0)
int remainder2 = 3 % 2; // (remainder variable is assigned to 1)
Yes it' the modulus.
http://en.wikipedia.org/wiki/Modulus_(algebraic_number_theory)
Let's say that
x / y = z,
x, y, z being integers.
There is no guarantee that
z * y = x, because the "/" operator rounds down.
So we must add a remainder to our equation:
z * y = x + r.
z * y = x + r
z * (-y) = - (z * y) = -(x + r) = -x - r
This means that the result of the "%" operator can be negative, which means that the "%" or remainder operator differs from the modulo relation, because the result is not guaranteed to be canonical.
Related
Is there a mod operator in C# for RSA algorithm? I've been using % as I thought this could be used as mod, but the answers I get for for c and m are not correct, so I've realised % doesn't work.
double e = 13;
double d; //decryption
double de = 7;
d = ((de * euiler) + 1) / e;
double message = 25;
double c = Pow(message, e) % n;
double m = Pow(c, d) % n;
The confusion lies in double Type.
MSDN:
The modulus operator (%) computes the remainder after dividing its first operand by its second. All numeric types have predefined modulus
operators.
Note the round-off errors associated with the double type.
the % is a remainder. You might want to make a static function that uses the % to make a modulo operation.
Assuming there's a float number with 7 digits how to find the smallest number that can be added to that float?
Example 1: 1234.567f + 0000.001f = 1234.568f
Example 2: 0.01234567 + 0.00000001f = 0.01234568f
OP added C# after posting of this C answer.
Will leave this C solution up as a reference.
Print the number out in exponential format and change each digit, to '0' or '1'
float smallest_float_summable(float f, int digs) {
char buf[20];
sprintf(buf, "%.*e", digs - 1, f);
char *p = buf;
while (*p != 'e') {
if (isdigit(*p)) {
*p = '0' + (p[1] == 'e');
}
p++;
}
float y;
sscanf(buf, "%e", &y);
return y;
}
printf("%e\n", smallest_float_summable(1234.567f, 7));
// 1.000000e-03
This will not get the smallest as the typically a number near 1/2 the value of smallest_float_summable() will effect the change, yet this seems to match OP's intent.
To get the smallest number that will effect some change, simple use nextafter()
The nextafter functions determine the next representable value, in the type of the function, after x in the direction of y ... C11dr §7.12.11.3 2
#include <math.h>
float smallest_change(float f) {
float dif = f - nextafterf(f, 0);
return dif;
}
[Edit]
#aka.nice correctly points out that even smaller, maybe about 1/2 dif will effect a change. Will ponder this.
To find the smallest epsilon, you could start with 10eN where N is 1, and move down to a smaller number and add it. Then compare it to the original number.
number = x
N = 1
newnumber = 3
while (number <> newnumber){
newnumber = (number + 10eN)
N = N - 1
}
Then 10e(N+1) is the smallest epsilon.
Today I was writing a program in C#, and I used % to calculate some index... My program didn't work, so I debugged it and I realized that "%" is not working like in other program languages that I know.
For example:
In Python % returns values like this:
for x in xrange (-5, 6):
print x, "% 5 =", x % 5
-5 % 5 = 0
-4 % 5 = 1
-3 % 5 = 2
-2 % 5 = 3
-1 % 5 = 4
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
In C#:
for (int i = -5; i < 6; i++)
{
Console.WriteLine(i + " % 5 = " + i % 5);
}
-5 % 5 = 0
-4 % 5 = -4
-3 % 5 = -3
-2 % 5 = -2
-1 % 5 = -1
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
Did I do something wrong or is % not working like it should?
As explained in the comments, the different behaviour is by design. The different languages just ascribe different meanings to the % operator.
You ask:
How can I use modulus operator in C#?
You can define a modulus operator yourself that behaves the same way as the Python % operator:
int mod(int a, int n)
{
int result = a % n;
if ((result<0 && n>0) || (result>0 && n<0)) {
result += n;
}
return result;
}
Both answers are correct. Although personally I think the "always positive" one makes more sense.
You can define your own modulus function that only gives positive answers like this:
int mod(int a, int n) {
return ((a%n)+n) % n;
}
In modular arithmetic, one defines classes of numbers based on the modulo. In other words, in modulo m arithmetic, a number n is equivalent (read: the same) to n + m, n - m, n + 2m, n - 2m, etc.
One defines m "baskets" and every number falls in one (and only one) of them.
Example: one can say "It's 4:30 pm" or one can say "It's 16:30". Both forms mean exactly the same time, but are different representations of it.
Thus both, the Python and C# results are correct! The numbers are the same in the modulo 5 arithmetic you chose. It would also have been mathematically correct to return (5, 6, 7, 8, 9) for example. Just a bit odd.
As for the choice of representation (in other words, the choice on how to represent negative numbers), that is just a case of different design choices between the two languages.
However, that is not at all what the % operator actually does in C#. The % operator is not the canonical modulus operator; it is the remainder operator. The A % B operator actually answer the question "If I divided A by B using integer arithmetic, what would the remainder be?"
— What's the difference? Remainder vs Modulus by Eric Lippert
Quick snippet to get the canonical modulus:
return ((n % m) + m) % m;
Test implementation:
Mono/C#:
machine:~ user$ cat mod.cs
using System;
public class Program
{
public static void Main (string[] args)
{
Console.WriteLine(Mod(-2, 5));
Console.WriteLine(Mod(-5, 5));
Console.WriteLine(Mod(-2, -5));
}
public static int Mod (int n, int m)
{
return ((n % m) + m) % m;
}
}
machine:~ user$ mono mod.exe
3
0
-2
Python:
machine:~ user$ cat mod.py
print -2%5;
print -5%5;
print -2%-5;
machine:~ user$ python mod.py
3
0
-2
Is there a library function in c# for the mathematical modulus of a number - by this I specifically mean that a negative integer modulo a positive integer should yield a positive result.
edited to provide an example:
-5 modulo 3 should return 1
Try (a % b) * Math.Sign(a)
Try this; it works correctly.
static int MathMod(int a, int b) {
return (Math.Abs(a * b) + a) % b;
}
x < 0 ? ((x % m) + m) % m : x % m;
Well the definition (if I'm not mistaken) is something like this
a mod b = a - b * floor(a/b)
It's probably pretty slow and beware of integer division just like built in modulus :)
Other option is to modify the result of built-in modulus according to the signs of operands. Something like this:
if(a < 0 && b > 0)
{
return (a % b + b) % b;
}
else if ....
a < 0 ? ((a+1)%b + b-1) : (a%b);
That requires only one % operation (and one ternary op) and no multiplication
If you're using any of these algorithms and you need to do division also, don't forget to make sure that you subtract 1 when appropriate.
I.e.,
if -5 % 2 = -1 and -5 / 2 = -2, and if you care that -5 / 2 * 2 + -5 % 2 = -5, then when you calculate -5 % 2 = 1, that you also calculate -5 / 2 = -3.
Fix :
(ans=a%b)<0 ? (a<0 && b<0 ? (ans-b)%(-b) : (ans+b)%b) : ans
I know the question didn't ask for it, but I just wrote and tested a method which returns the quotient as well. Didn't find this when I was looking for it, so I thought I'd put it out there.
/// <summary>
/// Compute integer quotient and remainder of <paramref name="dividend"/> / <paramref name="divisor"/>
/// where the <paramref name="remainder"/> has the same sign as <paramref name="divisor"/>, and is
/// between zero (inclusive) and the <paramref name="divisor"/> (exclusive). As always,
/// (quotientResult * <paramref name="divisor"/> + <paramref name="remainder"/> == <paramref name="dividend"/>).
/// </summary>
public static int DivRemPeriodic(int dividend, int divisor, out int remainder) {
var quotient = Math.DivRem(dividend, divisor, out remainder);
if (divisor > 0 ? remainder < 0 : remainder > 0) {
remainder += divisor;
quotient -= 1;
}
return quotient;
}
Might be the % operator?
http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx
how to find modulus of a number in c# .net?
With the modulus operator %:
int x = 4545;
int mod = x % 16;
Via the '%' operator.
e.g. int i = 10 % 3; (result: i is 1)
Use this site
http://www.willasrari.com/blog/use-c-modulus-operator-to-determine-even-or-odd-numbers/000166.aspx
Basically the % opearator: x%y