Mod operator in C# - c#

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.

Related

How can I have a double value using Math.DivRem()?

I can't show the double values in C#
Math.DivRem(double money, int number, out int remain);
Math.DivRem Method is only compatible with Int64 and Int32, which means you can't use double, float or any numeric data type with decimals.
Depending on what you are looking for, this is two other methods of getting the quotient and remainder of your doubles:
double a = 20.1;
double b = 4.93;
double remainder = a % b; // Returns 0.380...
double quotient = a / b; // Returns 4,077...
If you want to make your remainder or quotient to an int, just use
int result = Convert.ToInt32(quotient); // Returns 4
Or to make it more efficient
int result = ConvertToInt32(a / b); // Returns 4
EDIT
One workaround to use Math.DivRem with "doubles", are by multiplying both sides with a constant, for example 100 000. After getting the remainder, divide it with the constant.
double money = 5.515;
int number = 2;
int constant = 100000;
Math.DivRem(Convert.ToInt32(money * constant), number * constant, out int remain);
// Divides 551500 with 200000
// remain = 151500
// For double
double d = (double) remain / (double) constant; // Return 1,515
// For int
remain /= constant; // Return 1
DOUBLE EDIT
Use this instead of Math.DivRem() when dealing with doubles and ints combined.
double money = 12.15;
int number = 5;
double remainder = money % number; // Return 2,15...
int remainder = (int)(money % number); // Return 2

c# Math function not calulating

I have a simple math function
private int Doyle(LogArguments args)
{
return Convert.ToInt32(Math.Round(Math.Pow((args.Diameter - 4) , 2.0) * (args.Length / 16),0));
}
that should always return an int > 0 but my problem is if args.Length <= 16 it args.Length/16 = 0.
I have tried:
decimal d = args.length/16
and double d = args.length/16;
which both turn out to 0 when Length <16
If args.length is an int you are doing integer division regardless of what the type of the output variable is. To force decimal division the simplest way is to specify a decimal literal in the denominator:
decimal d = args.length / 16M;
Or for a double use a floating-point literal:
double d = args.length / 16.0;
double d = args.length/16;
which both turn out to 0 when Length <16
That is because args.Length and 16 are both integers. You get integer division (result 0), which is then cast to a double. Instead, try
double d = args.length/16.0;
If you are trying to multiply by a float value such as this:
5 / 16 = 0.3125
Then you should turn (args.Length / 16) into (args.Length / 16f).
This will force the division to be does as a floating point, and keep any remainder. Without the f added, it will do it as an integer division which truncates the remainder, so any value less than 16 will be less than 1 and get reduced to 0.

Power a large number with a large number then mode the result

I need to compute r = gkmod p where both g and k could be large integer, I mean they could be 64 bit integer. Is there any way?
You can take a look at the BigInteger.ModPow method. BigInteger can represent arbitrarily large integer values.
The ModPow method evaluates the following expression:
(baseValue ^ exponent) Mod modulus
Example from MSDN:
BigInteger number = 10;
int exponent = 3;
BigInteger modulus = 30;
Console.WriteLine("({0}^{1}) Mod {2} = {3}",
number, exponent, modulus,
BigInteger.ModPow(number, exponent, modulus));
//Result: (10^3) Mod 30 = 10
If you don't want to use that one but instead implement the operation yourself, there are certain techniques that can be used to do it efficiently.
In case you would prefer to stay with integers you can also implement your own ModularPower method:
public static int ModularPower(int baseVal, int expVal, int modVal)
{
int initialVal = 1;
for (int i = 0; i < expVal; ++i)
{
initialVal = (initialVal * baseVal) % modVal;
}
return initialVal;
}
Details of the algorithm and a pseudo code on Wikipedia.

What does % mean in C#?

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.

How can I improve this square root method?

I know this sounds like a homework assignment, but it isn't. Lately I've been interested in algorithms used to perform certain mathematical operations, such as sine, square root, etc. At the moment, I'm trying to write the Babylonian method of computing square roots in C#.
So far, I have this:
public static double SquareRoot(double x) {
if (x == 0) return 0;
double r = x / 2; // this is inefficient, but I can't find a better way
// to get a close estimate for the starting value of r
double last = 0;
int maxIters = 100;
for (int i = 0; i < maxIters; i++) {
r = (r + x / r) / 2;
if (r == last)
break;
last = r;
}
return r;
}
It works just fine and produces the exact same answer as the .NET Framework's Math.Sqrt() method every time. As you can probably guess, though, it's slower than the native method (by around 800 ticks). I know this particular method will never be faster than the native method, but I'm just wondering if there are any optimizations I can make.
The only optimization I saw immediately was the fact that the calculation would run 100 times, even after the answer had already been determined (at which point, r would always be the same value). So, I added a quick check to see if the newly calculated value is the same as the previously calculated value and break out of the loop. Unfortunately, it didn't make much of a difference in speed, but just seemed like the right thing to do.
And before you say "Why not just use Math.Sqrt() instead?"... I'm doing this as a learning exercise and do not intend to actually use this method in any production code.
First, instead of checking for equality (r == last), you should be checking for convergence, wherein r is close to last, where close is defined by an arbitrary epsilon:
eps = 1e-10 // pick any small number
if (Math.Abs(r-last) < eps) break;
As the wikipedia article you linked to mentions - you don't efficiently calculate square roots with Newton's method - instead, you use logarithms.
float InvSqrt (float x){
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i>>1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;}
This is my favorite fast square root. Actually it's the inverse of the square root, but you can invert it after if you want....I can't say if it's faster if you want the square root and not the inverse square root, but it's freaken cool just the same.
http://www.beyond3d.com/content/articles/8/
What you are doing here is you execute Newton's method of finding a root. So you could just use some more efficient root-finding algorithm. You can start searching for it here.
Replacing the division by 2 with a bit shift is unlikely to make that big a difference; given that the division is by a constant I'd hope the compiler is smart enough to do that for you, but you may as well try it to see.
You're much more likely to get an improvement by exiting from the loop early, so either store new r in a variable and compare with old r, or store x/r in a variable and compare that against r before doing the addition and division.
Instead of breaking the loop and then returning r, you could just return r. May not provide any noticable increase in performance.
With your method, each iteration doubles the number of correct bits.
Using a table to obtain the initial 4 bits (for example), you will have 8 bits after the 1st iteration, then 16 bits after the second, and all the bits you need after the fourth iteration (since a double stores 52+1 bits of mantissa).
For a table lookup, you can extract the mantissa in [0.5,1[ and exponent from the input (using a function like frexp), then normalize the mantissa in [64,256[ using multiplication by a suitable power of 2.
mantissa *= 2^K
exponent -= K
After this, your input number is still mantissa*2^exponent. K must be 7 or 8, to obtain an even exponent. You can obtain the initial value for the iterations from a table containing all the square roots of the integral part of mantissa. Perform 4 iterations to get the square root r of mantissa. The result is r*2^(exponent/2), constructed using a function like ldexp.
EDIT. I put some C++ code below to illustrate this. The OP's function sr1 with improved test takes 2.78s to compute 2^24 square roots; my function sr2 takes 1.42s, and the hardware sqrt takes 0.12s.
#include <math.h>
#include <stdio.h>
double sr1(double x)
{
double last = 0;
double r = x * 0.5;
int maxIters = 100;
for (int i = 0; i < maxIters; i++) {
r = (r + x / r) / 2;
if ( fabs(r - last) < 1.0e-10 )
break;
last = r;
}
return r;
}
double sr2(double x)
{
// Square roots of values in 0..256 (rounded to nearest integer)
static const int ROOTS256[] = {
0,1,1,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,
9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,
12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,
13,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
15,15,15,15,15,15,15,15,15,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16 };
// Normalize input
int exponent;
double mantissa = frexp(x,&exponent); // MANTISSA in [0.5,1[ unless X is 0
if (mantissa == 0) return 0; // X is 0
if (exponent & 1) { mantissa *= 128; exponent -= 7; } // odd exponent
else { mantissa *= 256; exponent -= 8; } // even exponent
// Here MANTISSA is in [64,256[
// Initial value on 4 bits
double root = ROOTS256[(int)floor(mantissa)];
// Iterate
for (int it=0;it<4;it++)
{
root = 0.5 * (root + mantissa / root);
}
// Restore exponent in result
return ldexp(root,exponent>>1);
}
int main()
{
// Used to generate the table
// for (int i=0;i<=256;i++) printf(",%.0f",sqrt(i));
double s = 0;
int mx = 1<<24;
// for (int i=0;i<mx;i++) s += sqrt(i); // 0.120s
// for (int i=0;i<mx;i++) s += sr1(i); // 2.780s
for (int i=0;i<mx;i++) s += sr2(i); // 1.420s
}
Define a tolerance and return early when subsequent iterations fall within that tolerance.
Since you said the code below was not fast enough, try this:
static double guess(double n)
{
return Math.Pow(10, Math.Log10(n) / 2);
}
It should be very accurate and hopefully fast.
Here is code for the initial estimate described here. It appears to be pretty good. Use this code, and then you should also iterate until the values converge within an epsilon of difference.
public static double digits(double x)
{
double n = Math.Floor(x);
double d;
if (d >= 1.0)
{
for (d = 1; n >= 1.0; ++d)
{
n = n / 10;
}
}
else
{
for (d = 1; n < 1.0; ++d)
{
n = n * 10;
}
}
return d;
}
public static double guess(double x)
{
double output;
double d = Program.digits(x);
if (d % 2 == 0)
{
output = 6*Math.Pow(10, (d - 2) / 2);
}
else
{
output = 2*Math.Pow(10, (d - 1) / 2);
}
return output;
}
I have been looking at this as well for learning purposes. You may be interested in two modifications I tried.
The first was to use a first order taylor series approximation in x0:
Func<double, double> fNewton = (b) =>
{
// Use first order taylor expansion for initial guess
// http://www27.wolframalpha.com/input/?i=series+expansion+x^.5
double x0 = 1 + (b - 1) / 2;
double xn = x0;
do
{
x0 = xn;
xn = (x0 + b / x0) / 2;
} while (Math.Abs(xn - x0) > Double.Epsilon);
return xn;
};
The second was to try a third order (more expensive), iterate
Func<double, double> fNewtonThird = (b) =>
{
double x0 = b/2;
double xn = x0;
do
{
x0 = xn;
xn = (x0*(x0*x0+3*b))/(3*x0*x0+b);
} while (Math.Abs(xn - x0) > Double.Epsilon);
return xn;
};
I created a helper method to time the functions
public static class Helper
{
public static long Time(
this Func<double, double> f,
double testValue)
{
int imax = 120000;
double avg = 0.0;
Stopwatch st = new Stopwatch();
for (int i = 0; i < imax; i++)
{
// note the timing is strictly on the function
st.Start();
var t = f(testValue);
st.Stop();
avg = (avg * i + t) / (i + 1);
}
Console.WriteLine("Average Val: {0}",avg);
return st.ElapsedTicks/imax;
}
}
The original method was faster, but again, might be interesting :)
Replacing "/ 2" by "* 0.5" makes this ~1.5 times faster on my machine, but of course not nearly as fast as the native implementation.
Well, the native Sqrt() function probably isn't implemented in C#, it'll most likely be done in a low-level language, and it'll certainly be using a more efficient algorithm. So trying to match its speed is probably futile.
However, in regard to just trying to optimize your function for the heckuvit, the Wikipedia page you linked recommends the "starting guess" to be 2^floor(D/2), where D represents the number of binary digits in the number. You could give that an attempt, I don't see much else that could be optimized significantly in your code.
You can try
r = x >> 1;
instead of / 2 (also in the other place you device by 2).
It might give you a slight edge.
I would also move the 100 into the loop. Probably nothing, but we are talking about ticks in here.
just checking it now.
EDIT:
Fixed the > into >>, but it doesn't work for doubles, so nevermind.
the inlining of the 100 gave me no speed increase.

Categories

Resources