jdf / Processing.py-Bugs

A home for all bugs and feature requests about Python Mode for the Processing Development Environment.
41 stars 8 forks source link

Cant use complex numbers in processing "pow()" function #371

Open flamingowrangler2869 opened 1 year ago

flamingowrangler2869 commented 1 year ago

Native python pow() can take complex numbers: >>> pow((0+1j), 2) squaring i (-1+0j) results in -1

However, processing's pow() cannot, and thus print(pow((0+1j), 2)) exact same function but in a print function results in nothing being printed and nothing else after that line will be run

And it's not print()'s fault, because print(complex(1,1)) results in (1+1j) and everything after is run

flamingowrangler2869 commented 1 year ago

Partially solved by making my own complex power function:

def cpow(z,n): # z^n where z ∈ ℂ, n ∈ ℤ
    re = z.real
    im = z.imag
    r = sqrt(re**2 + im**2)  # magnitude of z
    c = atan(im/re)  # angle of z
    y = r**n * (cos(n*c)+sin(n*c)*1j)  # r^n ⋅ (cos(nc)+isin(nc))
    return y

cpow(2+3j, 2) = -5.00000038632+11.9999998424j ≈ -5+12i = (2+3i)²