为什么大多数编程语言只给出 4 平方根的一个答案?

发布于 2024-07-20 06:08:39 字数 134 浏览 12 评论 0原文

大多数编程语言都将 2 作为 4 平方根的答案。 但是,有两个答案:2-2。 是否有任何特殊原因(历史或其他原因)为什么通常只给出一个答案?

Most programming languages give 2 as the answer to square root of 4. However, there are two answers: 2 and -2. Is there any particular reason, historical or otherwise, why only one answer is usually given?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

萤火眠眠 2024-07-27 06:08:40

这可能是因为当人们使用计算器计算平方根时,他们只想要正值。

更进一步,问问为什么计算器不允许你计算负数的平方根。 使用虚数是可能的,但普通用户对此的使用绝对为零。

关于虚数。

It's likely because when people use a calculator to figure out a square root, they only want the positive value.

Go one step further and ask why your calculator won't let you take the square root of a negative number. It's possible, using imaginary numbers, but the average user has absolutely zero use for this.

On imaginary numbers.

抠脚大汉 2024-07-27 06:08:39

因为:

  • 在数学中,除非另有说明,√x 通常指的是 x 的主(即正)根 [http://mathworld.wolfram.com/SquareRoot.html]
  • 某些语言无法返回多个值。
  • 由于您可以只应用否定,因此返回两者将是多余的。

Because:

  • In mathematics, √x commonly, unless otherwise specified, refers to the principal (i.e. positive) root of x [http://mathworld.wolfram.com/SquareRoot.html].
  • Some languages don't have the ability to return more than one value.
  • Since you can just apply negation, returning both would be redundant.
柏拉图鍀咏恒 2024-07-27 06:08:39

如果平方根方法返回两个值,那么这两个值之一实际上总是会被丢弃。 除了浪费内存和额外返回值的复杂性之外,它很少被使用。 大家都知道,可以将 -1 返回的答案相乘,得到另一个根。

我希望只有数学语言才会在这里返回多个值,也许作为数组或矩阵。 但对于大多数通用编程语言来说,按照您的建议进行操作的收益和成本都可以忽略不计。

If the square root method returned two values, then one of those two would practically always be discarded. In addition to wasting memory and complexity on the extra return value, it would be little used. Everyone knows that you can multiple the answer returned by -1 and get the other root.

I expect that only mathematical languages would return multiple values here, perhaps as an array or matrix. But for most general-purpose programming languages, there is negligible gain and non-negligible cost to doing as you suggest.

酒儿 2024-07-27 06:08:39

一些想法:

  • 历史上,函数被定义为返回单个值的过程。

  • 定义一个像这样返回多个值的干净函数会很繁琐(使用原始编程结构)。

  • 规则总有例外

    • 例如,0 仅具有单个根 (0)。
    • 不能对负数求平方根(除非该语言支持复数)。 在不支持虚数或复数系统的语言中,这可以被视为例外(例如“除以 0”)。
  • 通常很容易推导出 2 个平方根(简单地对函数返回的值求负)。 这可能是 sqrt() 函数的调用者留下的练习。

Some thoughts:

  • Historically, functions were defined as procedures which returned a single value.

  • It would have been fiddly (using primitive programming constructs) to define a clean function which returned multiple values like this.

  • There are always exceptions to the rule:

    • 0 for example only has a single root (0).
    • You cannot take the square root of a negative number (unless the language supports complex numbers). This could be treated as an exception (like "divide by 0") in languages which don't support imaginary numbers or the complex number system.
  • It is usually simple to deduce the 2 square roots (simply negate the value returned by the function). This was probably left as an exercise by the caller of the sqrt() function, if their domain depended on dealing with both the positive (+) and negative (-) roots.

空城之時有危險 2024-07-27 06:08:39

返回一个数字比返回两个数字更容易。 大多数工程决策都是以这种方式做出的。

It's easier to return one number than to return two. Most engineering decisions are made in this manner.

太阳哥哥 2024-07-27 06:08:39

有许多函数只能从 2 种或更多可能性中返回 1 个答案。 例如反正切。 1 的反正切返回为 45 度,但也可以是 225 甚至 405。与生活和编程中的许多事情一样,有一个我们知道并且可以依赖的约定。 平方根函数返回正值就是其中之一。 我们程序员有责任牢记还有其他解决方案,并在代码需要时采取行动。

顺便说一句,这是机器人技术中处理运动学和逆运动学方程时的一个常见问题,其中存在与笛卡尔位置相对应的连杆位置的多个解。

There are many functions which only return 1 answer from 2 or more possibilities. Arc tangent for example. The arc tangent of 1 is returned as 45 degrees, but it could also be 225 or even 405. As with many things in life and programming there is a convention we know and can rely on. Square root functions return positive values is one of them. It is up to us, the programmers, to keep in mind there are other solutions and to act on them if needed in code.

By the way this is a common issue in robotics when dealing with kinematics and inverse kinematics equations where there are multiple solutions of links positions corresponding to Cartesian positions.

染年凉城似染瑾 2024-07-27 06:08:39

在数学中,按照惯例,除非您明确另有说明,否则总是假设您想要某个值的正平方根。 四的平方根实际上是二。 如果你想要否定的答案,请在前面加上否定号。 如果两者都需要,请输入加号或减号。 如果没有这个约定,就不可能写出方程; 即使他们确实在前面放了一个符号,你也永远不会知道他们的意图(例如,因为它可能是负平方根的负数)。 另外,如果运算符开始返回两个值,您将如何编写涉及数学的任何类型的计算机代码? 它会破坏一切。

不幸的是,这个约定的一个例外是在求解变量时。 在下面的等式中:

x^2 = 4

您别无选择,只能考虑 X 的两个可能值。如果对两边取平方根,则得到 x = 2,但现在您必须输入加号或减号确保您没有错过任何可能的解决方案。 另外,请记住,在这种情况下,从技术上讲,X 可以是正数或负数,而不是四的平方根。

In mathematics, by convention it's always assumed that you want the positive square root of something unless you explicitly say otherwise. The square root of four really is two. If you want the negative answer, put a negative sign in front. If you want both, put the plus-or-minus sign. Without this convention it would be impossible to write equations; you would never know what the person intended even if they did put a sign in front (because it could be the negative of the negative square root, for example). Also, how exactly would you write any kind of computer code involving mathematics if operators started returning two values? It would break everything.

The unfortunate exception to this convention is when solving for variables. In the following equation:

x^2 = 4

You have no choice but to consider both possible values for X. if you take the square root of both sides, you get x = 2 but now you must put in the plus or minus sign to make sure you aren't missing any possible solutions. Also, remember that in this case it's technically X that can be either plus or minus, not the square root of four.

愿得七秒忆 2024-07-27 06:08:39

因为多个返回类型实现起来很烦人。 如果您确实需要另一个结果,只需将结果乘以 -1 不是很容易吗?

Because multiple return types are annoying to implement. If you really need the other result, isn't it easy enough to just multiple the result by -1?

夏末染殇 2024-07-27 06:08:39

因为大多数程序员只想要一个答案。

如果调用者需要的话,很容易从正值生成负值。 对于大多数代码,调用者仅使用正值。


然而,如今在许多语言中返回两个值都很容易。 在 JavaScript 中:

var sqrts=function(x) {
  var s=Math.sqrt(x);
  if (s>0) {
    return [s,-s];
  } else {
    return [0];
  }
}

只要调用者知道迭代返回的数组,你就是金子。

>sqrts(2)
[1.4142135623730951, -1.4142135623730951]

Because most programmers only want one answer.

It's easy enough to generate the negative value from the positive value if the caller wants it. For most code the caller only uses the positive value.


However, nowadays it's easy to return two values in many languages. In JavaScript:

var sqrts=function(x) {
  var s=Math.sqrt(x);
  if (s>0) {
    return [s,-s];
  } else {
    return [0];
  }
}

As long as the caller knows to iterate through the array that comes back, you're gold.

>sqrts(2)
[1.4142135623730951, -1.4142135623730951]
落叶缤纷 2024-07-27 06:08:39

我认为因为该函数称为“sqrt”,如果您想要多个根,则必须调用该函数“sqrts”,该函数不存在,所以您不能这样做。

更严肃的答案是你提出了一个更大问题的具体实例。 许多方程和常见的反函数(包括 sqrt)有多种可能的解,例如 arcsin 等,这些通常是一个问题。 例如,对于 arcsin,是否应该返回无限多个答案? 例如,请参阅有关分支削减的讨论。

I think because the function is called "sqrt", and if you wanted multiple roots, you would have to call the function "sqrts", which doesn't exist, so you can't do it.

The more serious answer is that you're suggesting a specific instance of a larger issue. Many equations, and commonly inverse functions (including sqrt) have multiple possible solutions, such as arcsin, etc, and these are, in general, an issue. With arcsin, for example, should one return an infinite number of answers? See, for example, discussions about branch cuts.

衣神在巴黎 2024-07-27 06:08:39

因为它在历史上被定义为给出已知曲面的正方形的边长的函数。 在这种情况下,长度是积极的。

Because it was historically defined{{citation needed}} as the function which gives the side length of a square of known surface. And length is positive in that context.

你的笑 2024-07-27 06:08:39

你总是可以知道另一个数字是多少,所以也许没有必要同时返回它们。

you can always tell what is the other number, so maybe it's not necessary to return both of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文