Euler's identity: Difference between revisions

m
m (→‎{{header|Bracmat}}: Added short comment)
m (→‎{{header|Wren}}: Minor tidy)
 
(20 intermediate revisions by 13 users not shown)
Line 3:
 
 
In mathematics,   ''Euler's identity''   (also known as   ''Euler's equation'')   is the equality:
 
<span style="font-size:150%;font-style:bold;"><span style="font-style:italic">e<sup>i<math>\pi</math></sup></span> + 1 = 0</span>
Line 27:
 
If that is the case, or there is some other limitation, show
that &nbsp; <big>e<sup>i<math>\pi</math></sup> + 1</big> &nbsp; is ''approximately'' equal to zero and
show the amount of error in the calculation.
 
If your language is capable of symbolic calculations, show
that &nbsp; <big>e<sup>i<math>\pi</math></sup> + 1</big> &nbsp; is ''exactly'' equal to zero for bonus kudos points.
<br><br>
 
Line 37:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">print(math:e ^ (math:pi * 1i) + 1)</langsyntaxhighlight>
 
{{out}}
Line 45:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Long_Complex_Text_IO; use Ada.Long_Complex_Text_IO;
with Ada.Numerics; use Ada.Numerics;
with Ada.Numerics.Long_Complex_Types; use Ada.Numerics.Long_Complex_Types;
Line 52:
begin
Put (Exp (Pi * i) + 1.0);
end Eulers_Identity;</langsyntaxhighlight>
 
{{out}}
Line 61:
<br>
We could use the identity exp(x + iy) = exp(x)( cos y + i sin y ), however the following uses a series expansion for exp(ix).
<langsyntaxhighlight lang="algol68">BEGIN
# calculate an approximation to e^(i pi) + 1 which should be 0 (Euler's identity) #
 
Line 113:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 120:
 
=={{header|Bracmat}}==
<syntaxhighlight lang Bracmat="bracmat">e^(i*pi)+1</langsyntaxhighlight>By symbolic calculation:<pre>0</pre>
 
=={{header|C}}==
Line 126:
 
The following code has been tested with gcc 5.4.0 on Ubuntu 16.04.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
#include <complex.h>
Line 139:
printf("e ^ %lci + 1 = [%.16f, %.16f] %lc 0\n", pi, creal(e), cimag(e), ae);
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 146:
</pre>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Numerics;
 
Line 158:
Console.WriteLine(Complex.Pow(e, i * π) + 1);
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 165:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <complex>
 
Line 171:
std::cout << std::exp(std::complex<double>(0.0, M_PI)) + 1.0 << std::endl;
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 181:
=={{header|Common Lisp}}==
Common Lisp has complex number arithmetic built into it.
<syntaxhighlight lang="common lisp">
<lang Common Lisp>
(+ 1 (exp (complex 0 pi)))
</syntaxhighlight>
</lang>
<pre>
#C(0.0L0 -5.0165576136843360246L-20)
</pre>
=={{header|Delphi}}==
{{libheader| System.VarCmplx}}
<syntaxhighlight lang="delphi">
program Euler_identity;
 
{$APPTYPE CONSOLE}
 
uses
System.VarCmplx;
 
begin
var result := VarComplexExp(Pi * VarComplexCreate(0, 1)) + 1;
writeln(result);
readln;
end.</syntaxhighlight>
{{out}}
<pre>0 + 0i</pre>
 
=={{header|F_Sharp|F#}}==
As per the discussion page this task as described is to show that -1+1=0
<syntaxhighlight lang="fsharp">
printfn "-1 + 1 = %d" (-1+1)
</syntaxhighlight>
{{out}}
<pre>
-1 + 1 = 0
</pre>
I shall also show that cos π = -1 and sin π = 0
<syntaxhighlight lang="fsharp">
printfn "cos(pi)=%f and sin(pi)=%f" (cos 3.141592653589793) (sin 3.141592653589793)
</syntaxhighlight>
{{out}}
<pre>
cos(pi)=-1.000000 and sin(pi)=0.000000
</pre>
I shall try the formula e<sup>iπ</sup>. I'll use the MathNet.Numerics package. I'll leave you the reader to determine what it 'proves'.
<syntaxhighlight lang="fsharp">
let i =MathNet.Numerics.complex(0.0,1.0);;
let pi=MathNet.Numerics.complex(MathNet.Numerics.Constants.Pi,0.0);;
let e =MathNet.Numerics.complex(MathNet.Numerics.Constants.E ,0.0);;
printfn "e**(i*pi) = %A" (e**(i*pi));;
</syntaxhighlight>
{{out}}
<pre>
e**(i*pi) = (-1, 1.2246467991473532E-16)
</pre>
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: math math.constants math.functions prettyprint ;
1 e pi C{ 0 1 } * ^ + .</langsyntaxhighlight>
{{out}}
<pre>
Line 196 ⟶ 241:
</pre>
 
=={{header|Forth}}==
Uses fs. (scientific) to print the imaginary term, since that's the one that's inexact.
<syntaxhighlight lang="forth">
." e^(i*π) + 1 = " pi fcos 1e0 f+ f. '+ emit space pi fsin fs. 'i emit cr
bye
</syntaxhighlight>
{{Out}}
<pre>
e^(i*π) + 1 = 0. + 1.22464679914735E-16 i
</pre>
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran">
program euler
use iso_fortran_env, only: output_unit, REAL64
Line 209 ⟶ 264:
write(output_unit,*) e**(pi*i) + 1
end program euler
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 219 ⟶ 274:
Provides complex arithmetic and a very rapidly converging algorithm for e^z.
 
<langsyntaxhighlight lang="freebasic">#define PI 3.141592653589793238462643383279502884197169399375105821
#define MAXITER 12
 
Line 320 ⟶ 375:
printc( curr )
print " Absolute error = ", absc(curr)
next i</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
import (
Line 333 ⟶ 388:
func main() {
fmt.Println(cmplx.Exp(math.Pi * 1i) + 1.0)
}</langsyntaxhighlight>
 
{{output}}
Line 343 ⟶ 398:
=={{header|Groovy}}==
Because the Groovy language does not provide a built-in facility for complex arithmetic, this example relies on the Complex class defined in the [[Complex_numbers#Groovy|Complex numbers]] example.
<langsyntaxhighlight lang="groovy">import static Complex.*
 
Number.metaClass.mixin ComplexCategory
Line 352 ⟶ 407:
println "e ** (π * i) + 1 = " + (e ** (π * i) + 1)
 
println "| e ** (π * i) + 1 | = " + (e ** (π * i) + 1).ρ</langsyntaxhighlight>
Output: (yadda, yadda, dust, yadda)
<pre>e ** (π * i) + 1 = 1.2246467991473532E-16i
Line 360 ⟶ 415:
 
A double is not quite real.
<langsyntaxhighlight Haskelllang="haskell">import Data.Complex
 
eulerIdentityZeroIsh :: Complex Double
Line 367 ⟶ 422:
main :: IO ()
main = print eulerIdentityZeroIsh</langsyntaxhighlight>
{{Out}}
 
Line 374 ⟶ 429:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang j>
NB. Euler's number is the default base for power
NB. using j's expressive numeric notation:
Line 397 ⟶ 452:
1 (=!.1e_15) ^ j. TAU
1
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
Since Java lacks a complex number class, a class is used that has sufficient operations.
<langsyntaxhighlight lang="java">
public class EulerIdentity {
 
Line 432 ⟶ 487:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 447 ⟶ 502:
be placed in a module to avoid name conflicts, and thus no special
prefix is used here.
<langsyntaxhighlight lang="jq">def multiply(x; y):
if (x|type) == "number" then
if (y|type) == "number" then [ x*y, 0 ]
Line 473 ⟶ 528:
 
def pi: 4 * (1|atan);
</syntaxhighlight>
</lang>
 
===The Task===
<langsyntaxhighlight lang="jq">"e^iπ: \( exp( [0, pi ] ) )",
"e^iπ + 1: \( plus(1; exp( [0, pi ] ) ))"</langsyntaxhighlight>
 
{{out}}
Line 487 ⟶ 542:
Julia has a builtin <tt>Complex{T}</tt> parametrized type.
 
<langsyntaxhighlight lang="julia">@show ℯ^(π * im) + 1
@assert ℯ^(π * im) ≈ -1</langsyntaxhighlight>
 
{{out}}
Line 495 ⟶ 550:
Using symbolic algebra, through the [https://github.com/chakravala/Reduce.jl Reduce.jl] package.
 
<langsyntaxhighlight lang="julia">using Reduce
@force using Reduce.Algebra
 
@show ℯ^(π * :i) + 1
@assert ℯ^(π * :i) + 1 == 0</langsyntaxhighlight>
 
{{out}}
Line 508 ⟶ 563:
 
e ^ πi is calculated by summing successive terms of the power series for e ^ x until the modulus of the difference between terms is no longer significant given the precision of the Double type (about 10 ^ -16).
<langsyntaxhighlight lang="scala">// Version 1.2.40
 
import kotlin.math.sqrt
Line 559 ⟶ 614:
e += Complex(1.0, 0.0)
println("e^${SMALL_PI}i + 1 = $e $APPROX_EQUALS 0")
}</langsyntaxhighlight>
 
{{output}}
Line 567 ⟶ 622:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{require lib_complex}
 
'{C.exp {C.mul {C.new 0 1} {C.new {PI} 0}}} // e^πi = exp( [π,0] * [0,1] )
-> (-1 1.2246467991473532e-16) // = -1
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">local c = {
new = function(s,r,i) s.__index=s return setmetatable({r=r, i=i}, s) end,
add = function(s,o) return s:new(s.r+o.r, s.i+o.i) end,
Line 585 ⟶ 640:
local one = c:new(1, 0)
local zero = i:mul(pi):exp():add(one)
print(string.format("e^(i*pi)+1 is approximately zero: %.18g%+.18gi", zero.r, zero.i))</langsyntaxhighlight>
{{out}}
<pre>e^(i*pi)+1 is approximately zero: 0+1.22460635382237726e-016i</pre>
<langsyntaxhighlight lang="lua">> -- alternatively, equivalent one-liner from prompt:
> math.exp(0)*math.cos(math.pi)+1, math.exp(0)*math.sin(math.pi)
0.0 1.2246063538224e-016</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">E^(I Pi) + 1</langsyntaxhighlight>
{{out}}
<pre>0</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
is(equal(%e^(%i*%pi)+1,0));
</syntaxhighlight>
{{out}}
<pre>
true
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import math, complex
 
echo "exp(iπ) + 1 = ", exp(complex(0.0, PI)) + 1, " ~= 0"</syntaxhighlight>
 
{{out}}
<pre>exp(iπ) + 1 = (0.0, 1.224646799147353e-16) ~= 0</pre>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml"># open Complex;;
# let pi = acos (-1.0);;
val pi : float = 3.14159265358979312
# add (exp { re = 0.0; im = pi }) { re = 1.0; im = 0.0 };;
- : Complex.t = {re = 0.; im = 1.22464679914735321e-16}</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use Math::Complex;
print exp(pi * i) + 1, "\n";</langsyntaxhighlight>
{{out}}
<pre>1.22464679914735e-16i</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include builtins\complex.e -- (0.8.0+)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
complex res = complex_add(complex_exp(complex_mul(PI,I)),1)
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #004080;">complex</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
?complex_sprint(res,both:=true)
<span style="color: #004080;">complex</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">complex_new</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
?complex_sprint(complex_round(res,1e16),true)
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">complex_add</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">complex_exp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">complex_mul</span><span style="color: #0000FF;">(</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
?complex_sprint(complex_round(res,1e15))</lang>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">complex_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">both</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">complex_sprint</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">complex_round</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e16</span><span style="color: #0000FF;">),</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">complex_sprint</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">complex_round</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e15</span><span style="color: #0000FF;">),</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
The actual result and two rounded versions (to prove the rounding is doing what it should - the second arg is an inverted precision).
Line 621 ⟶ 697:
"0+1.2246e-16i"
"0+1e-16i"
"0+0i"
</pre>
{{trans|Prolog}}
Of course "symbolically" you can just do this (ha ha):
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">reduce</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">rules</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"-1+1"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"-1+0"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-1"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"i*0"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"sin(pi)"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"cos(pi)"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-1"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"exp(i*pi)"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"cos(pi)+i*sin(pi)"</span><span style="color: #0000FF;">}}</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">seen</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> <span style="color: #000080;font-style:italic;">-- (be safe and avoid infinite loops)</span>
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">seen</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- "" re-treading</span>
<span style="color: #008080;">and</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">10000</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- "" ever-growing</span>
<span style="color: #000000;">seen</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">seen</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rules</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">found</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">reduce</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"exp(i*pi)+1"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
exp(i*pi)+1 = cos(pi)+i*sin(pi)+1
= -1+i*0+1
= -1+0+1
= -1+1
= 0
</pre>
 
=={{header|Prolog}}==
Symbolically manipulates Euler's identity until it can't be further reduced (and we get zero :)
<syntaxhighlight lang="prolog">
<lang Prolog>
% reduce() prints the intermediate results so that one can see Prolog "thinking."
%
Line 663 ⟶ 779:
 
simplify(X, X).
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 676 ⟶ 792:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">>>> import math
>>> math.e ** (math.pi * 1j) + 1
1.2246467991473532e-16j</langsyntaxhighlight>
 
=={{header|R}}==
<syntaxhighlight lang="r"># lang R
exp(1i * pi) + 1</syntaxhighlight>
{{out}}<pre>0+1.224606e-16i</pre>
 
Symbolically with the Ryacas package (on CRAN):
<syntaxhighlight lang="r">library(Ryacas)
as_r(yac_str("Exp(I * Pi) + 1"))</syntaxhighlight>
{{out}}<pre>0</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(+ (exp (* 0+i pi)) 1)</langsyntaxhighlight>
{{out}}<pre>0.0+1.2246063538223773e-016i</pre>
 
Line 692 ⟶ 818:
e, i and π are all available as built-in constants in Raku.
 
<syntaxhighlight lang="raku" perl6line>sub infix:<⁢> is tighter(&infix:<**>) { $^a * $^b };
 
say 'e**i⁢π + 1 ≅ 0 : ', e**i⁢π + 1 ≅ 0;
say 'Error: ', e**i⁢π + 1;</langsyntaxhighlight>
{{out}}
<pre>e**i⁢π + 1 ≅ 0 : True
Line 736 ⟶ 862:
of &nbsp; pi &nbsp; being defined within the REXX
<br>program.
<langsyntaxhighlight lang="rexx">/*REXX program proves Euler's identity by showing that: e^(i pi) + 1 ≡ 0 */
numeric digits length( pi() ) - length(.) /*define pi; set # dec. digs precision*/
cosPI= fmt( cos(pi) ) /*calculate the value of cos(pi). */
Line 758 ⟶ 884:
parse value format(x, 2, 1, , 0) 'E0' with g 'E' _ .; g= g * .5'e'_ % 2
do j=0 while h>9; m.j= h; h= h % 2 + 1; end
do k=j+5 to 0 by -1; numeric digits m.k; g= (g+x/g) *.5; end; return g || i</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 774 ⟶ 900:
<br>programmatically invoked with the requested number of decimal digits).
 
<langsyntaxhighlight lang="rexx">/*────────────────── 1,051 decimal digs of pi. ──────────────────*/
 
pi= 3.14159265358979323846264338327950288419716939937510
Line 796 ⟶ 922:
pi= pi || 59825349042875546873115956286388235378759375195778
pi= pi || 18577805321712268066130019278766111959092164201989
pi= pi || 38095257201065485863278865936153381827968230301952 </langsyntaxhighlight>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
'EXP(i*π)+1' →NUM ABS
{{out}}
<pre>
1.22464679915E-16
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">
include Math
Math::E ** (Math::PI * 1i) + 1
 
# => (0.0+0.0i)</lang>
E ** (PI * 1i) + 1
# => (0.0+0.0i)</syntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::f64::consts::PI;
 
extern crate num_complex;
Line 811 ⟶ 947:
fn main() {
println!("{:e}", Complex::new(0.0, PI).exp() + 1.0);
}</langsyntaxhighlight>
 
{{output}}
Line 821 ⟶ 957:
This example makes use of Spire's numeric data types. Complex takes a type parameter determining the type of the coefficients of a + bi, and Real is Spire's exact (i.e. arbitrary precision) numeric data type.
 
<langsyntaxhighlight lang="scala">import spire.math.{Complex, Real}
 
object Scratch extends App{
Line 831 ⟶ 967:
println(e.pow(pi*i) + one)
}</langsyntaxhighlight>
 
{{output}}
Line 837 ⟶ 973:
(0 + 0i)
</pre>
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<syntaxhighlight lang="scheme">; A way to get pi.
(define pi (acos -1))
 
; Print the value of e^(i*pi) + 1 -- should be 0.
(printf "e^(i*pi) + 1 = ~a~%" (+ (exp (* +i pi)) 1))</syntaxhighlight>
{{out}}
<pre>e^(i*pi) + 1 = 0.0+1.2246467991473532e-16i
</pre>
{{works with|Chez Scheme}}
A higher precision test using series approximations to Pi and the Exp() function.
The series are computed using exact rational numbers.
This code converts the rational results into decimal representation.
<syntaxhighlight lang="scheme">; Procedure to compute factorial.
 
(define fact
(lambda (n)
(if (<= n 0)
1
(* n (fact (1- n))))))
 
; Use series to compute approximation to Pi (using N terms of series).
; (Uses the Newton / Euler Convergence Transformation.)
 
(define pi-series
(lambda (n)
(do ((k 0 (1+ k))
(sum 0 (+ sum (/ (* (expt 2 k) (expt (fact k) 2)) (fact (1+ (* 2 k)))))))
((>= k n) (* 2 sum)))))
 
; Use series to compute approximation to exp(z) (using N terms of series).
 
(define exp-series
(lambda (z n)
(do ((k 0 (1+ k))
(sum 0 (+ sum (/ (expt z k) (fact k)))))
((>= k n) sum))))
 
; Convert the given Rational number to a Decimal string.
; If opt contains an integer, show to that many places past the decimal regardless of repeating.
; If opt contains 'nopar, do not insert the parentheses indicating the repeating places.
; If opt contains 'plus, prefix positive numbers with plus ('+') sign.
; N.B.: When number of decimals specified, this truncates instead of rounds.
 
(define rat->dec-str
(lambda (rat . opt)
(let* ((num (abs (numerator rat)))
(den (abs (denominator rat)))
(no-par (find (lambda (a) (eq? a 'nopar)) opt))
(plus (find (lambda (a) (eq? a 'plus)) opt))
(dec-lim (find integer? opt))
(rep-inx #f)
(rems-seen '())
(int-part (format (cond ((< rat 0) "-~d") (plus "+~d") (else "~d")) (quotient num den)))
(frc-list
(cond
((zero? num)
'())
(else
(let loop ((rem (modulo num den)) (decs 0))
(cond
((or (<= rem 0) (and dec-lim (>= decs dec-lim)))
'())
((and (not dec-lim) (assq rem rems-seen))
(set! rep-inx (cdr (assq rem rems-seen)))
'())
(else
(set! rems-seen (cons (cons rem decs) rems-seen))
(cons
(integer->char (+ (quotient (* 10 rem) den) (char->integer #\0)))
(loop (modulo (* 10 rem) den) (1+ decs))))))))))
(when (and rep-inx (not no-par))
(set! frc-list (append
(list-head frc-list rep-inx)
(list #\()
(list-tail frc-list rep-inx)
(list #\)))))
(if (null? frc-list)
int-part
(format "~a.~a" int-part (list->string frc-list))))))
 
; Convert the given Rational Complex number to a Decimal string.
; If opt contains an integer, show to that many places past the decimal regardless of repeating.
; If opt contains 'nopar, do not insert the parentheses indicating the repeating places.
; If opt contains 'plus, prefix positive numbers with plus ('+') sign.
; N.B.: When number of decimals specified, this truncates instead of rounds.
 
(define rat-cplx->dec-str
(lambda (rat-cplx . opt)
(let* ((real-dec-str (apply rat->dec-str (cons (real-part rat-cplx) opt)))
(imag-dec-str (apply rat->dec-str (cons (imag-part rat-cplx) (cons 'plus opt)))))
(format "~a~ai" real-dec-str imag-dec-str))))
 
; Print the value of e^(i*pi) + 1 -- should be 0.
; (Computed using the series defined above.)
 
(let*
((pi (pi-series 222))
(e-pi-i (exp-series (* pi +i) 222))
(euler-id (+ e-pi-i 1)))
(printf "e^(i*pi) + 1 = ~a~%" (rat-cplx->dec-str euler-id 70)))</syntaxhighlight>
{{out}}
<pre>e^(i*pi) + 1 =
0.0000000000000000000000000000000000000000000000000000000000000000000000
+0.0000000000000000000000000000000000000000000000000000000000000000000351i</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">say ('e**i⁢π + 1 ≅ 0 : ', Num.e**Num.pi.i + 1 ≅ 0)
say ('Error: ', Num.e**Num.pi.i + 1)</langsyntaxhighlight>
{{out}}
<pre>
Line 849 ⟶ 1,091:
=={{header|Tcl}}==
=== Using tcllib ===
<langsyntaxhighlight lang="tcl"># Set up complex sandbox (since we're doing a star import)
namespace eval complex_ns {
package require math::complexnumbers
Line 858 ⟶ 1,100:
set r [+ [exp [complex 0 $pi]] [complex 1 0]]
puts "e**(pi*i) = [real $r]+[imag $r]i"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 865 ⟶ 1,107:
 
=== Using VecTcl ===
<langsyntaxhighlight lang="tcl">package require vectcl
namespace import vectcl::vexpr
 
set ans [vexpr {pi=acos(-1); exp(pi*1i) + 1}]
puts "e**(pi*i) = $ans"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 877 ⟶ 1,119:
 
=={{header|Wren}}==
{{libheader|Wren-complex}}
As complex numbers are not built in, we create a bare-bones Complex class with only the methods we need to complete this task.
<lang ecmascript>class Num2 {
static exp(x) {
var e = 2.718281828459045
return e.pow(x)
}
}
 
<syntaxhighlight lang="wren">import "./complex" for Complex
class Complex {
construct new(real, imag) {
_real = real
_imag = imag
}
 
real { _real }
imag { _imag }
 
+ (other) { Complex.new(_real + other.real, _imag + other.imag) }
 
exp {
var re = Num2.exp(_real)
return Complex.new(re * _imag.cos, re * _imag.sin)
}
 
toString { (_imag >= 0) ? "%(_real) + %(_imag)i" : "%(_real) - %(-_imag)i" }
 
static one { Complex.new(1, 0) }
}
 
System.print((Complex.new(0, Num.pi).exp + Complex.one).toString)</langsyntaxhighlight>
 
{{out}}
Line 914 ⟶ 1,131:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
Z,pi,e := GSL.Z, (0.0).pi, (0.0).e;
 
println("e^(\u03c0i) + 1 = %s \u2245 0".fmt( Z(e).pow(Z(0,1)*pi) + 1 ));
println("TMI: ",(Z(e).pow(Z(0,1)*pi) + 1 ).format(0,25,"g"));</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits