0

I am just getting to learn python as my first language.

In python when I transfer the string type of input() to complex('a+b j') in order to try the addition, the answer of the addition is somehow weird.

I have tried addition of float(), the answer is exactly fit. But why complex weird. how can i fix that.

complex is weird but float is exactly fit.

I have tried this

aComplex = complex("10.11+2j")
bComplex = complex("10.2356+5j")
print(aComplex + bComplex)

aFloat = 10.11
bFloat = 10.2356
print(aFloat + bFloat)

aStrInput = input('a= ')
bStrInput = input('b= ')
aFloatInput = float(aStrInput)
bFloatInput = float(bStrInput)
print(aFloatInput + bFloatInput)

and get

(20.345599999999997+7j)
20.345599999999997
a= 10.11
b= 10.2356
20.345599999999997

thanks.~ so maybe the question can solve my problem Is floating point math broken? i will check it later.() thanks.

Square Y
  • 1
  • 2
  • If you have a problem with complex numbers, post a script as text that focuses just on that issue. We want something we can easily run, not a screenshot of some tool you are using. Don't prompt for input unless that is part of the problem. `aNumInput = complex("10.11+2j")` is better for test. – tdelaney Nov 25 '21 at 02:37
  • Wooo, so cool. but addition of float() is okay, while complex is weird. Is that normal? – Square Y Nov 25 '21 at 02:38
  • What do you mean by "weird"? Are you saying that you've lost some accuracy? What is the result you expect? – tdelaney Nov 25 '21 at 02:39
  • Adding like floats is: `10.11+10.2356` is `20.345599999999997` ... is that the weirdness? – tdelaney Nov 25 '21 at 02:45

0 Answers0