C # function to find the delta of two numbers

I just wanted to find out if there is anything built into the .net infrastructure where I can easily return the delta between two numbers? I wrote code that does this, but it looks like it should be within the framework already.

-3
math c #
Jul 10 '09 at 18:26
source share
9 answers
delta = Math.Abs(a - b); 
+60
Jul 10 '09 at 18:29
source share

I get the impression that "delta" is the difference between two numbers.

Until you tell me differently, I think you want:

 delta = Math.Abs(a - b); 
+18
Jul 10 '09 at 18:30
source share
 public static int Delta(int a, int b) { int delta = 0; if (a == b) { return 0; } else if (a < b) { while (a < b) { a++; delta++; } return delta; } else { while (b < a) { b++; delta++; } return delta; } } 

: R

Oh boy, I hope that no (future) employer will not meet this and will stop reading with disgust before he reaches the end of this post.

+15
Jul 10 '09 at 19:11
source share

Linq version (CLR 4.0 required).

(pops fingers, clears throat)

 var delta = (from t in Enumerable.Range(a, a).Zip(Enumerable.Range(b, b)) select Math.Abs(t.Item1 - t.Item2)) .First(); 
+11
Jul 22 '09 at 10:28
source share

Isn't that what the minus operator does ?: P

+9
Jul 10 '09 at 18:29
source share
 public static int Delta(int a, int b) { return a > 0? Delta(a-1, b-1) : a < 0 ? Delta(a+1, b+1) : b > 0 ? b : -b; } 

I think even better than the implementation of @JulianR Delta: -p

Editing: I did not understand that this was already proposed by @Robert Harvey, refers to it; -)

+8
Jul 11 '09 at 0:38
source share

What is the delta of two numbers? Delta has a certain meaning in set theory and infinitely small calculus, but this does not apply to numbers!

If you want to calculate the difference between the two numbers a and b, write |a - b| which is equal to Math. Abs (a - b) in C #.

+1
Jul 10 '09 at 18:31
source share

I decided to review the ridiculous JulianR answer above.

The code is shorter, but perhaps more complex:

 public static int Delta(int a, int b) { int delta = 0; while (a < b) { ++a; ++delta; } while (b < a) { ++b; ++delta; } return delta; } 

(for the faint of heart ... this is no more serious than the strange question that started the topic)

+1
Jul 11 '09 at 2:15
source share
 (r1+r2)/2 

Move between two numbers.

0
Aug 19 '16 at 12:29
source share



All Articles