sqrt()#

Calculates the square root of a number.

Examples#

example picture for sqrt()
1def setup():
2    py5.no_stroke()
3    a = py5.sqrt(6561)  # Sets 'a' to 81
4    b = py5.sqrt(625)   # Sets 'b' to 25
5    c = py5.sqrt(1)     # Sets 'c' to 1
6    py5.rect(0, 25, a, 10)
7    py5.rect(0, 45, b, 10)
8    py5.rect(0, 65, c, 10)
example picture for sqrt()
 1def setup():
 2    py5.no_stroke()
 3    a = py5.sqrt(6561)  # Sets 'a' to 81
 4    b = py5.sqrt(-625)   # Sets 'b' to the complex number (0+25j)
 5
 6    if isinstance(a, complex):
 7        py5.fill(255, 0, 0)
 8        py5.rect(0, 25, a.imag, 10)
 9    else:
10        py5.fill(255)
11        py5.rect(0, 25, a, 10)
12
13    if isinstance(b, complex):
14        py5.fill(255, 0, 0)
15        py5.rect(0, 45, b.imag, 10)
16    else:
17        py5.fill(255)
18        py5.rect(0, 45, b, 10)

Description#

Calculates the square root of a number. The square root of a positive number is always positive, even though there may be a valid negative root. The square root of a negative number is a complex number. In either case, the square root s of number a is such that s*s = a. It is the opposite of squaring.

Python supports complex numbers, but such values cannot be passed to py5 drawing functions. When using the sqrt() function, you should check if the result is complex before using the value. You can also extract the real and imaginary components of the complex value with .real and .imag. See the second example to learn how to do both of these things.

Syntax#

sqrt(value: Union[float, npt.NDArray]) -> Union[float, complex, npt.NDArray]

Parameters#

  • value: Union[float, npt.NDArray] - value to calculate the square root of

Updated on February 26, 2022 13:22:44pm UTC