views:

1760

answers:

5

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?

+15  A: 

You can do it like this

public decimal FindDifference(decimal nr1, decimal nr2)
{
  return Math.Abs(nr1 - nr2);
}
TT
Of course there is no need in putting this code into a separate function but it was more for clarifying
TT
+9  A: 
result = Math.Abs(value1 - value2);
Martin
He wanted to say Math. Math is the .Net class that contains static methods for trigonometric, logarithmic, and other common mathematical functions
Germstorm
Corrected - damn yanks with there silly spelling ;)
Martin
+1  A: 

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

johnc
Downvoted on this question!? Sense of humour anyone?
johnc
It is funny, but I would think about putting your humor in comments (or as community wiki) so you don't take hits on your rep. +1 for the hell of it.
paxdiablo
True, but I couldn't resist :)
johnc
I thought it was funny too ;-) But dry humour often gets missed in a written medium
MadKeithV
Comment of the year, +rep
qui
+1 cause I chuckled.
Learning
Someone who doesn't know the maths behind this solution isn't necessarily going to know if it's possible, especially when they have a working alternative. Dry humour is impossible to detect under those circumstances, so downvotes are entirely reasonable. Funny or not, it's wrong information.
JoeBloggs
use <humor> </humor>or a ;)
Ape-inago
Seriously, if someone doesn't know the mathematics of subtraction, perhaps they shouldn't be a developer ;/
johnc
The original question had a tag that said that a sense of humour was needed. In that context, all funny answers are allowed!
Lasse V. Karlsen
A: 

Basically you need a program like MATLAB or Mathematica to work out all the possible combinations, and then store them in a database.

Someone should host an online repository for these, as I'd imagine the database will be quite large.

Lasse V. Karlsen
O_o. It's _subtraction_. I really hope you're being sarcastic.
Nick Johnson
Well, the original question had a tag that said "requires-sense-of-humour", so yes. Since the question is, as you say, just subtraction, with an absolute function around it, then I gathered the person asking was looking for some "clever" solutions :)
Lasse V. Karlsen
+3  A: 

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). The first one is actually doing what Abs is working.

Mecki