Why do most programming languages only give one answer to square root of 4?
Asked Answered
W

12

16

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?

Worthy answered 8/5, 2009 at 4:17 Comment(4)
If you extend to deal with "return all roots" then you very quickly need a language with builtin support for complex numbers.Rossetti
And the name of that language is?Worthy
The name of the one of such languages is English. :)Infernal
@Infernal Is English a programming language? Or are you 'bait'-ing me?Worthy
D
37

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.
Di answered 8/5, 2009 at 4:23 Comment(10)
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).Winona
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.Inefficient
Pax, do you not see the reference provided? Go there and read the penultimate sentence in the paragraph above the pretty rainbows.Di
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.Macro
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 :-)Winona
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".Winona
Also note that the SAT, if that is any indicator of common math, always assumes +/-. :PMacro
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.Hyde
@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.Subsumption
Whooops, sorry, accidental down-vote as my mouse ran out of battery life. Then Stack Overflow decided to go all read-only. Bugger.Cabretta
G
17

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.

Gennagennaro answered 8/5, 2009 at 4:26 Comment(0)
P
12

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.

Pilocarpine answered 8/5, 2009 at 4:24 Comment(4)
Apropos square root of negative number: At least Lisp handles this and returns the complex number 0+1i: ? (sqrt -1) #C(0 1)Ardellearden
You mean: In languages that don't support imaginary numbers, it's not possible to take the square root of a negative number.Gennagennaro
I have enough trouble with numbers that aren't imaginaryChophouse
Imaginary numbers are just as real as real numbers: that is, not real at all.Mestas
E
10

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

Effortless answered 8/5, 2009 at 4:22 Comment(0)
G
9

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.

Gherardi answered 26/6, 2009 at 19:31 Comment(1)
I personally find this answer the most satisfying. +1.Dekow
J
8

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.

Jota answered 8/5, 2009 at 4:29 Comment(2)
"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.Orabelle
Wow Philip, that's a really harsh response.Adenine
M
5

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?

Macro answered 8/5, 2009 at 4:21 Comment(0)
H
4

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]
Harkey answered 23/6, 2009 at 22:50 Comment(0)
S
2

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.

Selinski answered 8/5, 2009 at 4:54 Comment(0)
C
2

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.

Cesarcesare answered 3/8, 2010 at 12:29 Comment(0)
G
1

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

Genteel answered 8/5, 2009 at 4:21 Comment(0)
I
1

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.

Inefficient answered 8/5, 2009 at 4:31 Comment(2)
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.Pilocarpine
Agree with the pragmatism. The imaginary numbers example was to show how impractical such a feature is for any normal user.Inefficient

© 2022 - 2024 — McMap. All rights reserved.