Difference between 2 numbers

I need the perfect algorithm or C# function to calculate the difference (distance) between 2 decimal numbers.

For example the difference between:
100 and 25 is 75
100 and -25 is 125
-100 and -115 is 15
-500 and 100 is 600

Is there a C# function or a very elegant algorithm to calculate this or I have to go and handle every case separately with ifs.

If there is such a function or algorithm, which one is it?

109389 次浏览

You can do it like this

public decimal FindDifference(decimal nr1, decimal nr2)
{
return Math.Abs(nr1 - nr2);
}
result = Math.Abs(value1 - value2);

I don't think it's possible in C#, you may need to look at implementing it in Assembler

Just adding this, as nobody wrote it here:

While you can surely use

Math.Abs(number1 - number2);

which is the easiest solution (and accepted answer), I wonder nobody wrote out what Abs actually does. Here's a solution that works in Java, C, C# and every other language with C like syntax:

int result = number1 - number2;
if (result < 0) {
result *= -1;
}

It's that simple. You can also write it like this:

int result = number1 > number2 ? number1 - number2 : number2 - number1;

The last one could be even faster once it got compiled; both have one if and one subtraction, but the first one has a multiplication in some cases, the last one has not. Why only in some cases? Some CPUs have a "swap sign" operation and the compiler recognizes what *= -1 does, it just swaps the sign, so instead of a multiplication, it will issue a swap sign operation for CPUs that offer it and this operation is as fast as a CPU operation can get (usually one clock cycle).

The first code example is actually doing what Abs is doing in most implementations to make use of "swap sign" where supported, the last one will be faster on CPUs that have no "swap sign" and were multiplications are more expensive than additions (on modern CPUs they are often equally fast).

This is how i do it in enterprise projects:

namespace Extensions
{
public class Functions
{
public static T Difference<T>(object x1, object x2) where T : IConvertible
{
decimal d1 = decimal.Parse(x1.ToString());
decimal d2 = decimal.Parse(x2.ToString());


return (T)Convert.ChangeType(Math.Abs(d1-d2), typeof(T));
}
}
}

and testing:

namespace MixedTests
{
[TestClass]
public class ExtensionsTests
{
[TestMethod]
public void Difference_int_Test()
{
int res2 = Functions.Difference<int>(5, 7);
int res3 = Functions.Difference<int>(-3, 0);
int res6 = Functions.Difference<int>(-3, -9);
int res8 = Functions.Difference<int>(3, -5);


Assert.AreEqual(19, res2 + res3 + res6 + res8);
}


[TestMethod]
public void Difference_float_Test()
{
float res2_1 = Functions.Difference<float>(5.1, 7.2);
float res3_1 = Functions.Difference<float>(-3.1, 0);
double res5_9 = Functions.Difference<double>(-3.1, -9);
decimal res8_3 = Functions.Difference<decimal>(3.1, -5.2);


Assert.AreEqual((float)2.1, res2_1);
Assert.AreEqual((float)3.1, res3_1);
Assert.AreEqual(5.9, res5_9);
Assert.AreEqual((decimal)8.3, res8_3);


}
}
}

this works for me :), returns the difference between two values. I have tested it and it has worked for me in all cases, also with decimals!!!

public static double Diference (double a,double b) {
return ((System.Math.Max(a,b)-System.Math.Min(a,b)));
}