Search code examples
programming-languageshistoryreturn-valuesqrtsquare-root

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


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?


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.