However, although it seems easy at first glance, it is a little troublesome to express it programmatically.
I have confirmed the execution with one of the following that can be compiled online. Paiza.io TIO
E
and $ π $ has a constant definition of PI
in std.math
.complex
template is available.^^
operator.python
import std;
void main()
{
auto x = complex(cast(real)0.0, PI);
auto e = complex(E, cast(real)0.0);
auto ans = e ^^ x;
writeln(ans);
writefln("%.10f %.10f", ans.re, ans.im);
}
Output result
-1-5.01656e-20i
-1.0000000000 -0.0000000000
If the operation result is output as it is like writeln (ans)
, an error of -1-5.01656e-20i
will occur.
As with other programming languages, this is what happens when you treat irrational numbers as floating point numbers.
By the way, in the case of D language, since the real
type, which is more accurate than double precision ( double
), can be used, the error of the imaginary part is less than 5.01656e-20
.
If you use the double
type, it will be -1 + 1.2465e-16i
like other programming languages.
In order to round the error and display it, writefln ("% .10f% .10f ", ans.re, ans.im)
and 10 digits after the decimal point are displayed.
In that case, the real part was displayed as -1.0000000000
and the imaginary part was displayed as -0.0000000000
.
The implementation examples of other programming languages are also introduced below.
$ e $ and $ π $ are not defined as constants in the C language standard specifications.
It may be defined as M_E
or M_PI
by the compiler.
For complex numbers, double complex
is prepared.
You can use the cpow
function for powers of complex numbers.
python
#include <stdio.h>
#include <math.h>
#include <complex.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_E
#define M_E 2.71828182845904523536
#endif
int main(void){
double complex x1, z;
x1 = M_PI * I;
z = cpow(M_E, x1);
printf("%.20f %.20f\n", creal(z), cimag(z));
printf("%.10f %.10f\n", creal(z), cimag(z));
}
Output result
-1.00000000000000000000 0.00000000000000012246
-1.0000000000 0.0000000000
It seems that the double complex
type cannot be displayed as it is in complex format.
M_E
and M_PI
in <cmath>
.complex
template is available.pow
function for exponentiation.python
#include <iostream>
#include <cmath>
#include <complex>
using std::complex;
int main(void){
auto x1 = complex<double>(0.0, M_PI);
auto e = complex<double>(M_E, 0.0);
auto z = pow(M_E, x1);
std::cout << z << std::endl;
printf("%.10f %.10f\n", real(z), imag(z));
}
Output result
(-1,1.22465e-16)
-1.0000000000 0.0000000000
Math.E
and Math.PI
.Complex
class is provided.Complex.Pow
function for powers of complex numbers.python
using System;
using System.Numerics;
public class Example
{
public static void Main()
{
Complex x = new Complex(0.0, Math.PI);
Complex ans = Complex.Pow(Math.E, x);
Console.WriteLine(ans);
Console.WriteLine("{0:f10} {1:f10}", ans.Real, ans.Imaginary);
}
}
Output result
(-1, 1.22464679914735E-16)
-1.0000000000 0.0000000000
math.E
and math.Pi
.complex
type is prepared.cmplx.Pow
function for powers of complex numbers.python
package main
import (
"fmt"
"math"
"math/cmplx"
)
func main() {
x1 := complex(0, math.Pi)
ans := cmplx.Pow(math.E, x1)
fmt.Println(ans)
fmt.Printf("%.10f %.10f", real(ans), imag(ans))
}
Output result
(-1+1.2246467991473515e-16i)
-1.0000000000 0.0000000000
1.0 + 1.0 * i
.**
operator.python
use Math::Complex;
my $pi = 3.14159265358979323846;
my $e = exp(1.0);
my $x = 0.0 + $pi * i;
my $ans = $e ** $x;
print $ans, "\n";
printf "%.10f %.10f\n", Re($ans), Im($ans);
Output result
-1+1.22464679914735e-16i
-1.0000000000 0.0000000000
math.e
and math.pi
.complex
type is prepared.pow
function for exponentiation.python
import math
x = complex(0, math.pi)
ans = pow(math.e, x)
print(ans)
print("%.10f %.10f" % (ans.real, ans.imag))
Output result
(-1+1.2246467991473532e-16j)
-1.0000000000 0.0000000000
Math :: E
and Math :: PI
.Complex
type is available.**
operator.python
x = Complex(0.0, Math::PI)
ans = Math::E ** x
puts(ans)
printf("%.10f %.10f\n", ans.real, ans.imag)
Output result
-1.0+0.0i
-1.0000000000 0.0000000000
In Ruby, the error was not displayed even if the calculation result was output as it was.
pi
.1.0: + 1.0
.**
operator.python
import Data.Complex
import Text.Printf
main = do
let x = 0.0 :+ pi
let e = exp 1.0
let ans = e ** x
let re = realPart(ans)
let im = imagPart(ans)
print ans
printf "%.10f %.10f\n" (re::Double) (im::Double)
Output result
(-1.0) :+ 1.2246467991473532e-16
-1.0000000000 0.0000000000
ℯ
and π
.Complex
type is available.im
is also provided as a constant.^
operator.python
using Printf
x = π * im # == complex(0.0, π)
ans = ℯ ^ x
println(ans)
@printf("%.10f %.10f\n", real(ans), imag(ans))
Output result
-1.0 + 1.2246467991473532e-16im
-1.0000000000 0.0000000000
Recommended Posts