I am new in python and I am trying to figure out how to round a number inside this function.
from math import *
import math
def complex_image(x,y,modulus):
phase1 = math.cos(math.radians(x))
phase2= math.sin(math.radians(y))
image = modulus * ( phase1 + phase2 * 1j)
return image
cx = complex_image(90,90,1)
print(f'The complex image is = {cx}')
The complex image is = (6.123233995736766e-17+1j)
The value it returned can just be counted as 0 + 1j
If I am to give this function the values complex_image(45,45,2/sqrt(2) it returns :
The complex image is = (1+0.9999999999999998j)
How would I write a condition that if for example the value for phase 1 is 6.123233995736766e-17 ( a number extremely close to 0 ) to just round it to 0 ?
I have tried this :
phase1 = round(math.cos(math.radians(x)))
phase2= round(math.sin(math.radians(y)))
But there's the chance that my result will just come up eronated because of this change , for example : complex_image(-45,-45,4/sqrt(2) for phase1,2 rounded comes up as :
The complex image is = (2.82842712474619-2.82842712474619j)
Without round, the result is :
The complex image is = (2-1.9999999999999996j)
Which is indeed the correct answer,but I would prefer it be rounded perfectly to 2-2j