[GO] Expressing "the most beautiful mathematical formula in the world" programmatically

Introduction

e^{iπ} = -1 Is Euler's equation, which is said to be "the most beautiful mathematical formula in the world". It's certainly beautiful or lean, but I wanted to output this programmatically. That's it.

However, although it seems easy at first glance, it is a little troublesome to express it programmatically.

Execution environment

I have confirmed the execution with one of the following that can be compiled online. Paiza.io TIO

For D language

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.

For C language

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.

For C ++

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

For C

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

For Go

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

For Perl

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

For Python

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

For Ruby

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.

For Haskell

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

For Julia

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

Expressing "the most beautiful mathematical formula in the world" programmatically
The most cited patent in the world
Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
Programming to fight in the world 5-3
Programming to fight in the world ~ 5-2
The most sought after programming language in 2020
Second-order symmetric decomposition in the Lie-Trotter formula
Get information on the 100 most influential tech Twitter users in the world with python.