Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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

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.

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.

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?

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]

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.

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top