How do I get jupyter Notebook to return the two possible values of a square root?

-1

I'm starting with Jupyter notebook in the race, and in practice I need to get the two values of a square root. That is, when I put sqrt (9), I returned 3 and -3. I have looked for solutions everywhere but none of them is useful! Thank you very much in advance !!

    
asked by Iván 10.10.2017 в 20:12
source

1 answer

0

One of the properties of the square root is that the result delivered is the absolute value of both roots. This value is the one delivered by Python in Jupyter.

Here you have the complete detail about the property: Taken from: link


Jupyter Notebook (Python Code):

If it is really necessary to work both values, I send you this function that will allow you to obtain an array with both results of the root:

import math

def sqrtTwoRoots(n):
    r = math.sqrt(n)
    return [[r],[-r]]

roots = sqrtTwoRoots(14)

for val in roots:
    print(val)

Here you can see the result:

I hope it will be useful for you. Greetings!

    
answered by 07.09.2018 в 19:09