Reference · Math
sqrt
signature
sqrt(num) -> floatWhat 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.0Edge cases
- The result is a float even for a perfect square:
sqrt(16)is4.0,intmakes it4. sqrt(-1)isNaN, which equals nothing, itself included: test it withmath.isnan.sqrt(0)is0.0.
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.0Errors
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 strthis 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.414See also
pow, math.sqrt, math.isnan, abs, round
Thread
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