3

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

kaya3
  • 47,440
  • 4
  • 68
  • 97
peterX
  • 33
  • 4
  • do you want to round it or just format the output to be rounded ? For actual value rounding see https://www.programiz.com/python-programming/methods/built-in/round. or https://stackoverflow.com/questions/1598579/rounding-decimals-with-new-python-format-function – Dr Phil Nov 12 '19 at 18:40
  • I just want to format the output to be rounded – peterX Nov 12 '19 at 18:42
  • Thank you very much , I have fixed my problem via your links , I will edit my main post with the solution , for future people – peterX Nov 12 '19 at 18:51
  • Don't edit the question to include the solution; simply mark the answer as accepted. If you've discovered a solution to your own problem, you can write your own answer and mark that as accepted. – kaya3 Nov 12 '19 at 19:08
  • Sorry I am new to this site , I will keep it in mind in the future ! – peterX Nov 12 '19 at 19:13
  • No worries; welcome to Stack Overflow. – kaya3 Nov 12 '19 at 19:14

2 Answers2

3
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)
    image = round(image.real, 8) + round(image.imag, 8)*1j
    return image

cx = complex_image(90,90,1)
cx = complex_image(45,45,2/sqrt(2))

print(f'The complex image is = {cx}')

round(num, b) can make num to accurate to b decimal places. Hope this resolves your problem. I think use round(num) directly can not solve your problem, as you will always get the integer for the complex.

1

You want to round the number at the end of your calculation; after multiplying by the modulus, not before.

If you're doing several different calculations with complex numbers, it may be useful to have a function just for rounding them.

def round_complex(z, ndigits=0):
    return round(z.real, ndigits) + 1j * round(z.imag, ndigits)

Usage:

>>> print('The complex image is', round_complex(cx, 8))
The complex image is (2-2j)
kaya3
  • 47,440
  • 4
  • 68
  • 97