2

Today I'm trying to prove the Euler's identity which is $e^{i\pi}+1=0$ by Python. My code is as follows:

euler = math.e
pi = np.pi

cos_pi = np.cos(pi) sin_pi = np.sin(pi) euler_id = euler*(imaginarypi)

print((cos_pi+imaginarysin_pi)+1) print('e^(ipi): %s' %eulers_id) # output = (-1+1.2246467991473532e-16j) print('e^(i*pi)+1 =: %s' %(eulers_id+1)) # output = 1.2246467991473532e-16j

What I get is an imaginary number, did I do something wrong here? Thank you for your answer.

Thomas Andrews
  • 177,126
  • 8
    The 1.2246467991473532e-16 makes the result a very small imaginary that "should be" zero. You're simply encountering an implementation issue with inexact floating-point arithmetic. – Blue Jul 31 '22 at 14:23
  • 3
    "Real" numbers such as $\pi$ and $e$ are idealized mathematical objects usually requiring infinite decimal expansions. Because we don't have the time to write infinite decimal expansions, when "real" number arithemetic is implemented in *real* life, there are usually roundoff errors. – Lee Mosher Jul 31 '22 at 14:39
  • 2
    This is the double-precision machine epsilon. A double-precision calculation, such as the one python is using here, cannot be more accurate than that epsilon. Thus, the result is consistent with zero. – eyeballfrog Jul 31 '22 at 14:41
  • 1
    @Blue Thank you for your answer. I solved this issue by using np.isclose((eulers_id+1), 0) – Kai-Chun Lin Jul 31 '22 at 14:42
  • 1
    In python, imaginary numbers can be written as $a*1j$. – xpaul Jul 31 '22 at 15:56
  • 1
    On the off chance it wasn't obvious, the final result is presented as $1.2246467991473532 \times 10^{-16} j$, not $1.2246467991473532e - 16j$, with $e$ being Euler's constant. :-) – Brian Tung Jul 31 '22 at 17:07
  • 2
    You might keep in mind: if you are truly interested in a rigorous proof of Euler's identity, a Python calculation, even one using np.isclose((eulers_id+1), 0), is not going to be sufficient. At best you're gathering calculational evidence, but you're certainly not getting a proof. – Lee Mosher Jul 31 '22 at 18:11
  • @Lee Mosher, yes, you're right. I use Python is only for a programming demonstration. :) – Kai-Chun Lin Jul 31 '22 at 18:45

0 Answers0