views:

1112

answers:

12

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?

A: 

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

cd1
+4  A: 

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?

Unknown
+10  A: 

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

jeffamaphone
+34  A: 

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.
Cirno de Bergerac
I don't think so! In maths, the solution to the equation f(x) = sqrt(x) is x = +/-2. It depends entirely on the domain you've chosen (natural numbers, integers, reals, etc). Since programs allow -ve numbers, maths would dictate that they return both solutions. It's pragmatism that limits the programming languages (although Python could always return a tuple).
paxdiablo
Additionally, most of the languages I can think off the top of my head could return a vector or at least some type of data structure to represent both values, if they wanted to.
Cuga
Pax, do you not see the reference provided? Go there and read the penultimate sentence in the paragraph above the pretty rainbows.
Cirno de Bergerac
How did this get 6 upvotes so fast? The first point doesn't make sense. In most programming languages, it's not √, its sqrt, which does not automatically mean the principle square root.
Unknown
I read it, sgm, but I also did university-level maths and I'm still very closely associated with mathematicians at the state university here. The link says "In common usage, ...". That is not what you said ("In mathematics"). Mathematicians are in a world of their own and they would most likely burn you at the stake for that comment :-)
paxdiablo
And I just noticed I stuffed up on my first comment. It should have said: "the solution to the equation y = sqrt(4) is y = +/-2".
paxdiablo
Also note that the SAT, if that is any indicator of common math, always assumes +/-. :P
Unknown
In mathematics different people use different notation. Just as you can have your holy war about whether Vim is better than Emacs you can also fight over the one *true* math notation.
Christian
@paxdiablo: In my formulary, the sqare root is defined to be a positive number. So Christian is probably right, there are different notations, and most programming languages just picked the one that fits their definition of a function better.
nikie
+12  A: 

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.

LeopardSkinPillBoxHat
Apropos square root of negative number: At least Lisp handles this and returns the complex number 0+1i:? (sqrt -1)#C(0 1)
Johan Kullbom
You mean: In languages that don't support imaginary numbers, it's not possible to take the square root of a negative number.
Eddie
Thanks - duly noted (and edited my answer appropriately).
LeopardSkinPillBoxHat
I have enough trouble with numbers that aren't imaginary
Ch00k
Imaginary numbers are just as real as real numbers: that is, not real at all.
TRiG
+13  A: 

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.

Eddie
+7  A: 

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.

Philip Brocoum
"In mathematics, by convention it's always assumed that you want the positive square root of something unless you explicitly say otherwise" - sorry this is 100% incorrect.
DanSingerman
Wow Philip, that's a really harsh response.
Albert
+1  A: 

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.

Cuga
The difference is that there is no REAL number which can be provided as the answer to "What is the square root of <some negative number>?". You need to use the COMPLEX number system.So I'm not sure how this is relevant to the question being asked. The fact is, there are (almost always) 2 REAL numbers which are valid square roots of a positive integer.Once again, I think it comes back to the domain. If the user is interested in both the + and - roots, they can derive them quite easily from what sqrt() returns. Sometimes pragmatism overrides correctness.
LeopardSkinPillBoxHat
Agree with the pragmatism. The imaginary numbers example was to show how impractical such a feature is for any normal user.
Cuga
+2  A: 

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.

tom10
+2  A: 

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]
Nosredna
+6  A: 

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.

Jim C
I personally find this answer the most satisfying. +1.
j_random_hacker
+2  A: 

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.

meduz