I want to find the absolute value of something like 'i' but when I type 'abs(1)' it says 'i' is not defined. What do I do?
Asked
Active
Viewed 909 times
-3
-
1*"when I type 'abs(1)' it says 'i' is not defined"* <- that seems unlikely. Do you mean when you type `abs(i)`? Because that's not a valid complex literal in Python, see e.g. https://docs.python.org/3/reference/lexical_analysis.html#imaginary-literals, it's just trying to refer to the name `i`. – jonrsharpe Feb 24 '18 at 22:41
1 Answers
1
In python to find the absolute value of a complex function you use j instead of i.
abs(a+bj) # General Format
abs(0+1j)
>> 1
Or you could define i as the square root of -1 and use it instead
i = (-1) ** 0.5
abs(i)
>> 1

Imaad F
- 128
- 8
-
@BennnHBennnH also FYI https://stackoverflow.com/help/someone-answers – jonrsharpe Feb 24 '18 at 22:42
-
@imaad, your second example should be `(-1)**0.5`, right now you're doing `-(1**0.5)` as can be seen checking the value of `i` itself rather than `abs(i)`, (it gives `-1` rather than `1j`) – Nick is tired Feb 25 '18 at 05:35