

 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.




BEGIN
    # calculate an approximation to e^(i pi) + 1 which should be 0 (Euler's identity) #
 
    # returns e^ix for long real x, using the series:                                 #
    #      exp(ix) = 1 - x^2/2! + x^4/4! - ... + i(x - x^3/3! + x^5/5! - x^7/7! ... ) #
    #      the expansion stops when successive terms differ by less than 1e-15        #
    PROC expi = ( LONG REAL x )LONG COMPL:
         BEGIN
            LONG REAL t              := 1;
            LONG REAL real part      := 1;
            LONG REAL imaginary part := 0;
            LONG REAL divisor        := 1;
            BOOL      even power     := FALSE;
            BOOL      subtract       := FALSE;
            LONG REAL diff           := 1;
            FOR n FROM 1 WHILE ABS diff > 1e-15 DO
                divisor *:= n;
                t       *:= x;
                LONG REAL term := t / divisor;
                IF even power THEN
                    # this term is real #
                    subtract := NOT subtract;
                    LONG REAL prev := real part;
                    IF subtract THEN
                        real part -:= term
                    ELSE
                        real part +:= term
                    FI;
                    diff := prev - real part
                ELSE
                    # this term is imaginary #
                    LONG REAL prev := imaginary part;
                    IF subtract THEN
                        imaginary part -:= term
                    ELSE
                        imaginary part +:= term
                    FI;
                    diff := prev - imaginary part
                FI;
                even power := NOT even power
            OD;
            ( real part, imaginary part )
         END # expi # ;
    LONG COMPL eulers identity = expi( long pi ) + 1;
    print( ( "e^(i*pi) + 1 ~ "
           , fixed( re OF eulers identity, -23, 20 )
           , " "
           , fixed( im OF eulers identity,  23, 20 )
           , "i"
           , newline
           )
         )
END