Polar Coordinates

Sort by

recency

|

468 Discussions

|

  • + 1 comment
    from math import sqrt,atan,pi
    
    z=complex(input())
    
    x=z.real 
    y=z.imag 
    
    r=sqrt(x**2+y**2)
    
    if x<0 and y>=0:
        theta=atan(y/x)+pi
    elif x<0 and y<0:
        theta=atan(y/x)-pi
    elif x>0 and y<0:
        theta=atan(y/x)
    elif x>0 and y>=0:
        theta=atan(y/x)
    elif x==0 and y>0:
        theta=pi/2
    elif x==0 and y<0:
        theta=-pi/2
    else:
        theta=0
    
    print(f'{r}\n{theta}')
    
  • + 1 comment

    In barely few lines...

    import cmath
    complex_num = complex(input())
    print(abs(complex_num))
    print(cmath.phase(complex_num))
    
  • + 0 comments
    from cmath import phase
    z = input()
    print(abs(complex(z)), phase(complex(z)), sep='\n')
    
  • + 1 comment

    I didn't understand why they suggested the used of abs function in the question, here's my solution:

    import cmath
    import math
    valor = input()
    complejo = complex(valor)
    partes = None
    real = None
    coeficienteimaginario = None
    if(valor.find("+") > 0):
        partes = valor.split("+")
        real = float(partes[0])
        coeficienteimaginario = float(partes[1].replace("j",""))
    elif(valor.startswith("-")):
        partes = valor.split("-")
        real = float("-"+partes[1])
        coeficienteimaginario = float(partes[2].replace("j",""))
    else:
        partes = valor.split("-")
        real = float(partes[0])
        coeficienteimaginario = float(partes[1].replace("j",""))
    r = math.sqrt(pow(real,2)+pow(coeficienteimaginario,2))
    print(r)
    ph = cmath.phase(complejo)
    print(ph)
    
  • + 0 comments

    No one suggested a one-liner yet?

    print(*cmath.polar(complex(input())), sep='\n')