

 This page uses content from Wikipedia. The original article was at Euler's_identity. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)



In mathematics, Euler's identity is the equality:

               ei
  
    
      
        π
      
    
    {\displaystyle \pi }
  
 + 1 = 0

where

   e is Euler's number, the base of natural logarithms,
   i is the imaginary unit, which satisfies i2 = −1, and
   
  
    
      
        π
      
    
    {\displaystyle \pi }
  
 is pi, the ratio of the circumference of a circle to its diameter.

Euler's identity is often cited as an example of deep mathematical beauty. Three of the basic arithmetic operations occur exactly once each: addition, multiplication, and exponentiation. The identity also links five fundamental mathematical constants:

   The number 0.
   The number 1.
   The number 
  
    
      
        π
      
    
    {\displaystyle \pi }
  
 (
  
    
      
        π
      
    
    {\displaystyle \pi }
  
 = 3.14159+),
   The number e (e = 2.71828+), which occurs widely in mathematical analysis.
   The number i, the imaginary unit of the complex numbers.

Task
Show in your language that Euler's identity is true. As much as possible and practical, mimic the Euler's identity equation.

Most languages are limited to IEEE 754 floating point calculations so will have some error in the calculation.

If that is the case, or there is some other limitation, show
that ei



π


{\displaystyle \pi }

 + 1 is approximately equal to zero and
show the amount of error in the calculation.

If your language is capable of symbolic calculations, show
that ei



π


{\displaystyle \pi }

 + 1 is exactly equal to zero for bonus kudos points.




#define PI 3.141592653589793238462643383279502884197169399375105821
#define MAXITER 12
 
'---------------------------------------
'   complex numbers and their arithmetic
'---------------------------------------
 
type complex
    r as double
    i as double
end type
 
function conj( a as complex ) as complex
    dim as complex c
    c.r =  a.r
    c.i = -a.i
    return c
end function
 
operator + ( a as complex, b as complex ) as complex
    dim as complex c
    c.r = a.r + b.r
    c.i = a.i + b.i
    return c
end operator
 
operator - ( a as complex, b as complex ) as complex
    dim as complex c
    c.r = a.r - b.r
    c.i = a.i - b.i
    return c
end operator
 
operator * ( a as complex, b as complex ) as complex
    dim as complex c
    c.r = a.r*b.r - a.i*b.i
    c.i = a.i*b.r + a.r*b.i
    return c
end operator
 
operator / ( a as complex, b as complex ) as complex
    dim as double bcb = (b*conj(b)).r
    dim as complex acb = a*conj(b), c 
    c.r = acb.r/bcb
    c.i = acb.i/bcb
    return c
end operator
 
sub printc( a as complex )
    if a.i>=0 then
        print using "############.############### + ############.############### i"; a.r; a.i
    else
        print using "############.############### - ############.############### i"; a.r; -a.i
    end if
end sub
 
function intc( n as integer ) as complex
    dim as complex c
    c.r = n
    c.i = 0.0
    return c
end function
 
function absc( a as complex ) as double
    return sqr( (a*conj(a)).r )
end function
 
'-----------------------
' the algorithm
' Uses a rapidly converging continued
' fraction expansion for e^z and recursive
' expressions for its convergents
'-----------------------
 
dim as complex pii, pii2, curr, A2, A1, A0, B2, B1, B0
dim as complex ONE, TWO
dim as integer i, k = 2
pii.r = 0.0
pii.i = PI
pii2 = pii*pii
 
B0 = intc(2)
A0 = intc(2)
B1 = (intc(2) - pii)
A1 = B0*B1 + intc(2)*pii
printc( A0/B0)
print "     Absolute error = ", absc(A0/B0)
printc( A1/B1)
print "     Absolute error = ", absc(A1/B1)
 
for i = 1 to MAXITER
    k = k + 4
    A2 = intc(k)*A1 + pii2*A0
    B2 = intc(k)*B1 + pii2*B0
    curr = A2/B2
    A0 = A1
    A1 = A2
    B0 = B1
    B1 = B2
    printc( curr  )
    print "     Absolute error = ", absc(curr)
next i