I built a calculator for a bio equation, and I think I've narrowed down the source of my problem, which is a natural log I take: Show
My problem is that it will only display the output in a complex form:
Is there any way of getting this to print as a floating number? Nothing I've tried has worked.
cel 28.3k16 gold badges90 silver badges113 bronze badges asked Feb 16, 2016 at 4:43
Complex numbers have a real part and an imaginary part:
It looks like you just want the real part... so:
answered Feb 16, 2016 at 4:44
mgilsonmgilson 288k60 gold badges601 silver badges674 bronze badges 1 Use Since you don't want the imaginary part of the result, it would be better to use ie, use:
answered Feb 16, 2016 at 4:58
tom10tom10 64.6k9 gold badges122 silver badges134 bronze badges 4 If you want to just convert it to float value you can do this:
In this way you can actually get the floating value. answered Jul 3, 2018 at 13:39
It is simple if you need only real part of the complex number.
If you need absolute value then use following
dazito 7,45915 gold badges72 silver badges112 bronze badges answered Feb 23 at 20:53
1
The complex number has two parts – real and imaginary. The imaginary part is denoted with “j” suffix. How to find the type of a Number?We can find the type of number using the type() function. print(type(x)) print(type(y)) print(type(z)) Output: Python Numbers1. IntegerIntegers are whole numbers. They can be positive or negative. They must be without decimal values. We can use int() function to get the integer representation of an object. The object must implement __int__() method that returns an integer. Let’s look into some examples of creating integers in Python. x = 10 print(type(x)) x = int("10") print(type(x)) class Data: id = 0 def __init__(self, i): self.id = i def __int__(self): return self.id d = Data(10) x = int(d) print(x) print(type(x)) Output: <class 'int'> <class 'int'> 10 <class 'int'> String class provides __int__() method, that’s why we can easily convert a string to int using the int() method. If the object doesn’t implement __int__() method, the int() function throws TypeError. Python Int TypeErrorGenerally, integers are defined on base 10. But, we can also define them in binary, octal, and hexadecimal format. i = 0b1010 print(i) # 10 i = 0xFF print(i) # 255 i = 0o153 print(i) # 107 A floating point number contain decimal points. It can be positive or negative. We can use float() function to get the float representation of an object. The object must implement __float__() method that returns a floating point number. x = 10.50 print(x) print(type(x)) x = float("10.50") print(x) print(type(x)) class Data: id = 0.0 def __init__(self, i): self.id = i def __float__(self): return float(self.id) d = Data(10.50) x = float(d) print(x) print(type(x)) d = Data(10) x = float(d) print(x) print(type(x)) Output: 10.5 <class 'float'> 10.5 <class 'float'> 10.5 <class 'float'> 10.0 <class 'float'> String provides __float__() method implementation. That’s why we can easily convert a string to float. If the object doesn’t implement__float__() method, we get the error message as: TypeError: float() argument must be a string or a number, not 'Data' If the object __float__() method doesn’t return a floating point number, we get the error message as: TypeError: Data.__float__ returned non-float (type int) We can also define a float in scientific notation using “e” or “E”. Here the number after “E” specifies the power to 10. x = 10.5e2 print(x) x = 10.5E2 print(x) Output: Explanation: 10.5E2 = 10.5 * pow(10, 2) = 10.5 * 100 = 1050.0 3. ComplexA complex number contains two part – real and imaginary. The imaginary part is written with the “j” suffix. We can also use the complex() function to create a complex number. We can pass two ints or float arguments to the complex() function. The first argument is the real part and the second argument is the complex part. x = 1 + 2j print(x) print(type(x)) x = -1 - 4j print(x) print(type(x)) x = complex(1, 2) print(x) print(type(x)) x = complex(1) print(x) print(type(x)) x = complex(-1, -2.5) print(x) print(type(x)) Output: (1+2j) <class 'complex'> (-1-4j) <class 'complex'> (1+2j) <class 'complex'> (1+0j) <class 'complex'> (-1-2.5j) <class 'complex'> We can also get an object complex number representation by defining __complex__() method. This method must return a complex number. class Data: def __init__(self, r, i): self.real = r self.imaginary = i def __complex__(self): return complex(self.real, self.imaginary) d = Data(10, 20) c = complex(d) print(c) print(type(c)) Output: Python Complex NumberWe can also convert a string to a complex number. There should not be any white space between the real and the imaginary part. c = complex("1+2j") # works fine c = complex("1 + 2j") # ValueError: complex() arg is a malformed string We can get the real part of the complex number using the “real” property. We can get the imaginary part of the complex number using the “imag” property. c = 10 + 20j print(c.real) # real part print(c.imag) # imaginary part Some other complex number methods are:
c = 1 + 2j print(c.conjugate()) # (1-2j) print(abs(c)) # 2.23606797749979 Python Numbers Type ConversionWe can convert an int to float using the float() function. Similarly, we can use int() function to convert a float to int. We can use complex() function to convert an int or float to the complex number, the imaginary part will be 0j. We can’t convert a complex number to int or float. i = 10 f = 10.55 # int to float conversion f1 = float(i) print(f1) print(type(f1)) # float to int conversion i1 = int(f) print(i1) print(type(i1)) # int and float to complex number conversion c = complex(i) print(c) print(type(c)) c = complex(f) print(c) print(type(c)) Output: 10.0 <class 'float'> 10 <class 'int'> (10+0j) <class 'complex'> (10.55+0j) <class 'complex'> ConclusionNumbers are an integral part of any programming language. Python support three types of numbers – int, float, and complex. Numbers in python are also objects of type – int, float, and complex. We can convert an object to number using the int(), float(), and complex() functions. The complex number is mostly used in the geometry and scientific calculations. References:
How do you convert complex to int?Type int(x) to convert x to a plain integer. Type long(x) to convert x to a long integer. Type float(x) to convert x to a floating-point number. Type complex(x) to convert x to a complex number with real part x and imaginary part zero.
How do you convert to complex in Python?Converting real numbers to complex number
An complex number is represented by “ x + yi “. Python converts the real numbers x and y into complex using the function complex(x,y). The real part can be accessed using the function real() and imaginary part can be represented by imag().
How do you take complex numbers as inputs in Python?Method 1- Take a sting as input then convert it into complex. Method 2- Take two separate numbers and then convert them into complex. a = input() # user will enter 3+5j a = complex(a) # then this will be converted into complex number.
Can Python handle complex numbers?Since complex is a native data type in Python, you can plug complex numbers into arithmetic expressions and call many of the built-in functions on them. More advanced functions for complex numbers are defined in the cmath module, which is part of the standard library.
|