why sqrt is not a built-in function?

Chris Angelico rosuav at gmail.com
Thu Jan 14 14:01:18 EST 2021


On Fri, Jan 15, 2021 at 5:56 AM <2QdxY4RzWzUUiLuE at potatochowder.com> wrote:
>
> On 2021-01-14 at 17:54:55 +0000,
> Alan Gauld via Python-list <python-list at python.org> wrote:
>
> > My question is: why do we even have a sqrt() in the
> > math module given that pow() and ** are already there?
>
> Probably because the standard C math library has such a function, and
> Python's math module is (or at least was) supposed be a thin wrapper
> around that library.
>
> For completeness, C doesn't have a exponentiation operator.

Also, the math module works specifically with floats. Sometimes that's
what you want, other times it's not.

>>> pow(-2, 0.5)
(8.659560562354934e-17+1.4142135623730951j)
>>> (-2)**0.5
(8.659560562354934e-17+1.4142135623730951j)
>>> from math import sqrt; sqrt(-2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> from cmath import sqrt; sqrt(-2)
1.4142135623730951j

ChrisA


More information about the Python-list mailing list