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:
goldman = [[R * T] / F] * cmath.log[float[top_row] / float[bot_row]]
print["Membrane potential: " + str[goldman] + "V"
My problem is that it will only display the output in a complex form:
Membrane potential: [0.005100608207126714+0j]V
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:
>>> c = complex[1, 0]
>>> c
[1+0j]
>>> c.real
1.0
It looks like you just want the real part... so:
print["Membrane potential: " + str[goldman.real] + "V"
answered Feb 16, 2016 at 4:44
mgilsonmgilson
288k60 gold badges601 silver badges674 bronze badges
1
Use math.log
instead of cmath.log
.
Since you don't want the imaginary part of the result, it would be better to use math.log
instead of cmath.log
. This way, you'll
get an error if your input is not in the valid domain for a real log
, which is much better than silently giving you meaningless results [for example, if top_row
is negative]. Also, complex numbered results make no sense for this particular equation.
ie, use:
goldman = [[R * T] / F] * math.log[float[top_row] / float[bot_row]]
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:
def print_float[x1]:
a=x1.real
b=x1.imag
val=a+b
print[val]
ec=complex[2,3]
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.
goldman = 0.005100608207126714+0j
print[goldman]
[0.005100608207126714+0j]
goldman = float[goldman.real]
print[goldman]
0.005100608207126714
If you need absolute value then use following
goldman = 0.005100608207126714+0j
print[goldman]
[0.005100608207126714+0j]
goldman = float[abs[goldman]]
print[goldman]
0.005100608207126714
print[type[goldman]]
dazito
7,44915 gold badges72 silver badges112 bronze badges
answered Feb 23 at 20:53
1
How to Create a Number Variable in Python?
x = 10
y = 10.55
z = 1 + 2j
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:
1. Integer
Integers 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:
10
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.
Generally, 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 10.5 10.5 10.0
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. Complex
A 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] [-1-4j] [1+2j] [1+0j] [-1-2.5j]
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:
We 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:
- conjugate[]: returns the complex conjugate number. The sign of the imaginary part is reversed.
- abs[]: returns the magnitude of the complex number.
c = 1 + 2j print[c.conjugate[]] # [1-2j] print[abs[c]] # 2.23606797749979
Python Numbers Type Conversion
We 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 10 [10+0j] [10.55+0j]
Conclusion
Numbers 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:
- Python Numeric Literals API Doc