

In case one, the test-maker, in writing the question, uses this symbol. For example, in the questions above, we know +4 is a square root of 16, but can’t -4 be one as well? Or can it? Can a square root be negative? Do we include the negative square root as part of Column B or not? Does it matter how the question is framed? All of these questions about possible negative square roots are resolved by understanding the following two cases. Often, students are confused about this question. The distinction between them is the subject of this article.Įxplanations to these practice problems will appear at the end of this blog article.
#Square root of a negative number how to
This example illustrates (1) the potential subtlety of order of operations (2) there’s more than one way to do it (3) how to experiment/look for help (4) read/interpret error messages.All I will say right now is: despite apparent similarities, those two questions about whether roots are positive or negative, have two completely different answers. 2.72+0.65*i), but it would only be worth it if I were working with complex variables a lot.Īnd now, just after I’ve finished with this whole thing, I’ve realized that there was a shortcut all along: print(2.72+0.65j) # (2.72+0.65j) If I really wanted to, I could set i (or j) equal to complex(0,1), or to pow(-1,0.5), and then define complex variables via real_part+i*imag_part (e.g. (This kind of problem is an inevitable consequence of trying to approximate real numbers via finite binary approximations we will talk about it more in a few weeks.) The R language has a zapsmall function that sets small values to zero the numpy module for Python has a real_if_close() function, but that’s not quite what we want (we want to set the real part to zero). Python tells me the answer is \(\epsilon+i\), where \(\epsilon\) is a tiny number (not quite zero). They don’t give me exactly what I wanted, though. These both work ( but only in Python 3: Python 2 gives “negative error cannot be raised to a fractional power”). Oh well, it looks like the math module is really only designed for floating-point operations, not operations on complex numbers …Ĭoming back to what (in hindsight) I should have done in the first place, I tried (1) the pow() function (which takes care of the order-of-operations problem pow() is definitely applied after the unary minus sign and (2) making sure I put parentheses around the (-1) when using the ** operator. Just out of curiosity, what happens if I try math.sqrt() on a number that’s already defined as complex? math.sqrt(complex(-1,0)) # Traceback (most recent call last): That showed me that there is indeed a pow function for complex numbers (admittedly this is a little hard to read!), but that’s not really the point – I want to take pow() of a negative floating point number with a fractional power. What if I try reading the help file? help("complex") Apparently math.sqrt defines a function whose domain is restricted to non-negative values (that’s what math domain error means). Math.sqrt(-1) # Traceback (most recent call last): Using a little bit of additional information (I knew/guessed that there is an extra library, or module, in Python called math, and that the following is the way to access it): import math # load the math module Then I guessed that there might be a square-root function called sqrt(): this is common in a lot of computer languages sqrt(-1) # Traceback (most recent call last):

This didn’t work due to a logical but surprising glitch: the operator precedence (i.e., the order in which operators get executed) is higher for the power operator ** than for the unary minus operator. It took me a little while, but the way that I tried to get there may be interesting.įirst, I tried to take the square root of -1: print(-1**0.5) # -1.0 I was wondering whether there was an easier way or a shortcut for writing complex(a,b) to get a complex number of the form \(a+bi\) (or \(a+bj\), in keeping with Python’s use of j for \(\sqrt\). Square roots of negative numbers Ben Bolker 15:33 12 January 2015
