Aug-09-2022, 09:44 AM
(This post was last modified: Aug-09-2022, 09:44 AM by Gribouillis.)
(Aug-09-2022, 09:20 AM)Estherbarnes Wrote: (x**0.5) isn't the same as square root, it gives another result, doesn't it?When x is a nonnegative float, it should give the same result, but it turns out that for negative floats, Python returns the principal complex square root for
x**0.5
while sqrt(x)
raises a math domain error. So these are not exactly the same functions.>>> (-1)**0.5 (6.123233995736766e-17+1j) >>> from math import sqrt >>> sqrt(-1) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: math domain errorFor a general object, the expression
x ** 0.5
returns x.__pow__(0.5)
, so the result depends on how the class of x defines the __pow__()
method, which can be very arbitrary.