Reference · Math

math.sqrt

signature
math.sqrt(num) -> float

What it does

The square root of a number, as a float, the way Python's math.sqrt gives it. An int is fine as input; the answer is a float even for a perfect square. A negative number is a runtime error, where the plain sqrt gives NaN.

Examples

print(math.sqrt(16), math.sqrt(2), math.sqrt(0.25))print(math.sqrt(1e10), math.sqrt(2) * math.sqrt(2))
output
4.0 1.4142135623730951 0.5100000.0 2.0000000000000004

Edge cases

print(math.sqrt(0), math.sqrt(-0.0))print(math.sqrt(math.inf), math.sqrt(math.nan))print(math.sqrt(2) * math.sqrt(2) == 2, math.isclose(math.sqrt(2) * math.sqrt(2), 2))print(math.sqrt(9007199254740993), math.isqrt(9007199254740993))
output
0.0 -0.0inf NaNfalse true94906265.62425156 94906265

Errors

A negative number is a runtime error, not an error value: it stops the request, and ?? does not catch it, so test the sign first when a negative input is possible. The plain sqrt(-1) is NaN instead. A value that is not a number is a runtime error too; a decimal is converted first with float.

this one fails
print(math.sqrt(-1))
output
line 1: `math.sqrt`: math domain error (-1)
this one fails
print(math.sqrt(dec("4")))
output
line 1: `math.sqrt` expects a number as argument 1, got dec (this works on floats: convert a dec with float(x))
print(sqrt(-1), math.sqrt(float(dec("4"))))
output
NaN 2.0

Chaining

In a pipeline the number comes first, so math.sqrt takes it as its only argument. For a whole list, vec.sqrt does the same in one call:

print(3 * 3 + 4 * 4 | math.sqrt)print([1, 4, 9] | map(math.sqrt), vec.sqrt([1, 4, 9]))
output
5.0[1.0, 2.0, 3.0] [1.0, 2.0, 3.0]

See also

sqrt, math.cbrt, math.pow, math.isqrt, math.hypot, vec.sqrt

Thread
7Useful? Like it, or add to its thread below.

Thread 0 posts

On math.sqrt(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo