Reference · Math

sqrt

signature
sqrt(num) -> float

What it does

The square root, always a float: sqrt(16) is 4.0. A negative number gives NaN, not an error; math.sqrt is the version that errors, as Python does.

Examples

print(sqrt(16), sqrt(2), sqrt(2.25))print(sqrt(pow(3, 2) + pow(4, 2)))
output
4.0 1.4142135623730951 1.55.0

Edge cases

print(type_of(sqrt(16)), int(sqrt(16)))print(sqrt(-1), sqrt(-1) == sqrt(-1), math.isnan(sqrt(-1)))print(sqrt(0))
output
float 4NaN false true0.0

Errors

A string, null or a boolean is a runtime error, not an error value, and so is a decimal: convert it with float first.

this one fails
print(sqrt("4"))
output
line 1: `sqrt` expects a number as argument 1, got str
this one fails
print(sqrt(4m))
output
line 1: `sqrt` expects a number as argument 1, got dec (this works on floats: convert a dec with float(x))

Chaining

print(16 | sqrt, [1, 4, 9] | map(sqrt))print(2 | sqrt | round(3))
output
4.0 [1.0, 2.0, 3.0]1.414

See also

pow, math.sqrt, math.isnan, abs, round

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

Thread 0 posts

On 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