Euler's formula in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Euler's formula in python

The great mathematician Euler discovered a fascinating mathematical formulaz according to which "e to the power of pi i is -1". [e^(πi)=-1] I tried to do this in python by the command pow(math.e, math.pi*1j) But the output isn't -1. Can anyone tell me how to do it the correct way? Thanks.

20th Jan 2019, 2:41 PM
Avish Kathpal
Avish Kathpal - avatar
12 Answers
20th Jan 2019, 4:11 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 7
Avish Excellent! 👌
29th Jan 2019, 12:18 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 5
Avish check it out now. I added formatting to show that it is zero indeed. Or kind of :)
22nd Jan 2019, 12:30 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 4
but i is imaginary number...
20th Jan 2019, 2:57 PM
Hubert Dudek
Hubert Dudek - avatar
+ 4
Python doesn't know or use the actual values of pi or e, that's why calculating with them will always lead to (negligible) errors
20th Jan 2019, 3:36 PM
Anna
Anna - avatar
+ 2
The output is -1 + a very small number which you can assume it is 0 as this is a computer and not an actual mathematician (problem with computers and irrational numbers, long story)
20th Jan 2019, 3:18 PM
Mohamed Ali ZORGATI
Mohamed Ali ZORGATI - avatar
+ 2
Kuba SiekierzyƄski I just found out a better way of doing it. Hope you like it. https://code.sololearn.com/cVFrOVv5Tvw4/?ref=app
29th Jan 2019, 11:51 AM
Avish Kathpal
Avish Kathpal - avatar
+ 1
Avish Kathpal I don't. In Python 1j means the square root of -1.
20th Jan 2019, 6:39 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 1
Kuba SiekierzyƄski Thanks for you help.
22nd Jan 2019, 2:22 PM
Avish Kathpal
Avish Kathpal - avatar
0
Kuba SiekierzyƄski , why do you neglect the imaginary part?
20th Jan 2019, 4:14 PM
Avish Kathpal
Avish Kathpal - avatar
0
At the end of the number you will see e-16 Which means 10^-16 which is a very small number
20th Jan 2019, 4:49 PM
Mohamed Ali ZORGATI
Mohamed Ali ZORGATI - avatar
0
Kuba SiekierzyƄski Then have you written .real() there? It is used to just print the real part and not the imaginary part.
22nd Jan 2019, 9:56 AM
Avish Kathpal
Avish Kathpal - avatar