C#函数求两个数的delta

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1111194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:29:28  来源:igfitidea点击:

C# function to find the delta of two numbers

c#math

提问by Bryan Corazza

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.

我只是想知道 .net 框架中是否有任何内容可以轻松返回两个数字之间的增量?我编写了执行此操作的代码,但听起来应该已经包含在框架中。

采纳答案by Robert Harvey

delta = Math.Abs(a - b);

回答by jalf

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

这不是减号运算符的作用吗?:p

回答by abelenky

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

我的印象是“delta”是两个数字之间的差异。

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

除非你以不同的方式告诉我,否则我认为你想要的是:

delta = Math.Abs(a - b);

回答by Dario

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!

两个数的delta是多少?Delta 在集合论和无穷小微积分中有一定的意义,但这不是指数字!

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#.

如果要计算两个数字 a 和 b 之间的差值,请在 C# 中编写|a - b|which 。Math.Abs(a - b)

回答by JulianR

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

:p

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

哦,男孩,我希望没有(未来的)雇主遇到这个并在他到达这篇文章的结尾之前停止厌恶地阅读。

回答by fortran

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

我认为这比@JulianR Delta 实现还要好:-p

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

编辑:我没有意识到@Robert Harvey 已经提出了这一建议,归功于他 ;-)

回答by abelenky

I decided to revise JulianR's funny answer above.

我决定修改上面 JulianR 的有趣回答。

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)

(对于幽默受损的人......这并不比开始线程的奇怪问题更严重)

回答by Daniel Earwicker

The Linq version (requires CLR 4.0).

Linq 版本(需要 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();

回答by Petar

(r1+r2)/2

Avarage between two numbers.

两个数字之间的平均值。