-1

I just wanted to know if there's anything built into the .net framework where I can easily return the delta between two numbers? I wrote code that does this but it sounds like something that should be in the framework already.

2
  • 1
    Is this question determining whether floats are within a given range ? (otherwise it's more or less a subtraction problem...
    – nos
    Jul 10, 2009 at 18:31
  • 22
    Subtraction . . . addition's tricky pal.
    – Michael
    Jul 10, 2009 at 18:34

9 Answers 9

65
delta = Math.Abs(a - b);
5
  • 7
    Wow...a 24 character answer netted 385 rep (and counting). That's not a bad ratio.
    – Aaron
    Jan 9, 2012 at 2:39
  • 2
    Also netted him a Reversal badge. Only 207 of these ever handed out! Nice!
    – Alveoli
    Apr 15, 2015 at 9:06
  • Word of extreme caution: This code will crash when a-b is equal to Int32.MinValue because of System.Overflow exception. More details here.
    – RBT
    Mar 13, 2018 at 0:00
  • 1
    @RBT: Many functions in the Math class are subject to overflow or underflow. It's up to the caller of these methods to know what their limitations are. Mar 13, 2018 at 2:31
  • @RobertHarvey you are absolutely correct. I investigated bit more on why this happens. The reason turned out to be very trivial as mentioned here. The range of number data types is always like -x to (x-1). Of course, if I try to accomodate +x in the same data type to obtain the absolute value it is bound fail :)
    – RBT
    Mar 13, 2018 at 10:09
18

I'm under the impression that "delta" is the difference between two numbers.

Until you tell me differently, I think what you want is:

delta = Math.Abs(a - b);
14
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;
  }
}

:p

Oh boy, I hope no (future) employer comes across this and stops reading in disgust before he reaches the end of this post..

0
11

The Linq version (requires CLR 4.0).

(cracks 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();
9

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

1
  • 2
    Not exactly but nice answer anway ;-)
    – Dario
    Jul 10, 2009 at 18:32
8
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 that's even better than @JulianR Delta implementation :-p

Edit: I didn't realize that this was already suggested by @Robert Harvey, credit to him ;-)

2
  • 3
    Oh, that's much better. Recursion and chained ternary operators!
    – nobody
    Jul 11, 2009 at 0:40
  • Yeah, and it even works! XD I thought that I'd have made some mistake, but surprisingly not ^_^
    – fortran
    Jul 11, 2009 at 0:53
1

What is the delta of two numbers? Delta has a certain meaning in set-theory and infinitesimal calculus, but this doesn't refer to numbers!

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

2
  • 1
    Delta does not have a certain meaning in infinitesimal calculus, it is merely a commonly used symbol.
    – jason
    Jul 10, 2009 at 18:51
  • 1
    Jason, in calculus delta may refer to Diracs delta function and hence has a well defined meaning. A different definition that is widely used is Kroneckers delta. Hence Dario is right, when he asks for a clarification. The OPs question is somewhat ambiguous. Jul 11, 2009 at 15:46
1

I decided to revise JulianR's funny answer above.

The code is shorter, but perhaps more tricky:

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 humor-impaired.... this is no more serious than the bizarre question that started the thread)

0
(r1+r2)/2

Avarage between two numbers.

Not the answer you're looking for? Browse other questions tagged or ask your own question.