Skip to content

Instantly share code, notes, and snippets.

@minoki
Last active March 22, 2017 16:39
Show Gist options
  • Save minoki/826ec60a7a17081074664fc6ebe001a8 to your computer and use it in GitHub Desktop.
Save minoki/826ec60a7a17081074664fc6ebe001a8 to your computer and use it in GitHub Desktop.
Test programs for complex numbers in various languages
#include <stdio.h>
#include <math.h>
#include <complex.h>
#if !defined(CMPLX)
# if defined(__clang__)
# define CMPLX(x,y) ((double _Complex){(x), (y)})
# elif defined(__GNUC__)
# define CMPLX(x,y) __builtin_complex((double)(x), (double)(y))
# endif
#endif
int main(int argc, char *argv[]) {
puts("C _Complex");
printf("abs(1e300 + 1e300 i) = %.16g\n", cabs(CMPLX(1e300, 1e300)));
{
double complex z = 1.0 / CMPLX(1e300, 1e300);
printf("1 / (1e300 + 1e300 i) = %.16g + %.16g i\n", creal(z), cimag(z));
}
printf("abs(inf + nan i) = %g\n", cabs(CMPLX(INFINITY, NAN)));
{
double complex z = CMPLX(1.0, 1.0) * CMPLX(INFINITY, NAN);
printf("(1 + 1 i) * (inf + nan i) = %g + %g i\n", creal(z), cimag(z));
}
{
double complex z = CMPLX(INFINITY, NAN) * CMPLX(INFINITY, NAN);
printf("(inf + nan i) * (inf + nan i) = %g + %g i\n", creal(z), cimag(z));
}
{
double complex z = 1.0 / CMPLX(INFINITY, NAN);
printf("1 / (inf + nan i) = %g + %g i\n", creal(z), cimag(z));
}
{
double complex z = 1.0 / CMPLX(0.0, 0.0);
printf("1 / (0 + 0 i) = %g + %g i\n", creal(z), cimag(z));
}
{
double complex z = CMPLX(INFINITY, NAN) / CMPLX(1.0, 2.0);
printf("(inf + nan i) / (1 + 2 i) = %g + %g i\n", creal(z), cimag(z));
}
}
#include <stdio.h>
#include <math.h>
#include <complex>
int main(int argc, char *argv[]) {
using std::complex;
puts("C++ std::complex");
printf("abs(1e300 + 1e300 i) = %.16g\n", abs(complex<double>(1e300, 1e300)));
{
complex<double> z = 1.0 / complex<double>(1e300, 1e300);
printf("1 / (1e300 + 1e300 i) = %.16g + %.16g i\n", real(z), imag(z));
}
printf("abs(inf + nan i) = %g\n", abs(complex<double>(INFINITY, NAN)));
{
complex<double> z = complex<double>(1.0, 1.0) * complex<double>(INFINITY, NAN);
printf("(1 + 1 i) * (inf + nan i) = %g + %g i\n", real(z), imag(z));
}
{
complex<double> z = complex<double>(INFINITY, NAN) * complex<double>(INFINITY, NAN);
printf("(inf + nan i) * (inf + nan i) = %g + %g i\n", real(z), imag(z));
}
{
complex<double> z = 1.0 / complex<double>(INFINITY, NAN);
printf("1 / (inf + nan i) = %g + %g i\n", real(z), imag(z));
}
{
complex<double> z = 1.0 / complex<double>(0.0, 0.0);
printf("1 / (0 + 0 i) = %g + %g i\n", real(z), imag(z));
}
{
complex<double> z = complex<double>(INFINITY, NAN) / complex<double>(1.0, 2.0);
printf("(inf + nan i) / (1 + 2 i) = %g + %g i\n", real(z), imag(z));
}
}
package main
import (
"fmt"
"math"
"math/cmplx"
)
func main() {
fmt.Println("Go")
fmt.Println("abs(1e300 + 1e300 i) =", cmplx.Abs(1e300+1e300i))
fmt.Println("1 / (1e300 + 1e300 i) =", 1 / (1e300 + 1e300i))
fmt.Println("abs(inf + nan i) =", cmplx.Abs(complex(math.Inf(0), math.NaN())))
fmt.Println("(1 + 1 i) * (inf + nan i) =", complex(1, 1) * complex(math.Inf(0), math.NaN()))
fmt.Println("(inf + nan i) * (inf + nan i) =", complex(math.Inf(0), math.NaN()) * complex(math.Inf(0), math.NaN()))
fmt.Println("1 / (inf + nan i) =", 1 / complex(math.Inf(0), math.NaN()))
var zero = complex(0, 0)
fmt.Println("1 / (0 + 0 i) =", 1 / zero)
fmt.Println("(inf + nan i) / (1 + 2 i) =", complex(math.Inf(0), math.NaN()) / (1 + 2i))
}
import Prelude
import Data.Complex
inf, nan :: Double
inf = 1 / 0
nan = 0 / 0
main = do
putStrLn "Haskell Data.Complex"
putStr "abs(1e300 + 1e300 i) = "
print $ magnitude (1e300 :+ 1e300)
putStr "1 / (1e300 + 1e300 i) = "
print $ 1 / (1e300 :+ 1e300)
putStr "abs(inf + nan i) = "
print $ magnitude (inf :+ nan)
putStr "(1 + 1 i) * (inf + nan i) = "
print $ (1 :+ 1) * (inf :+ nan)
putStr "(inf + nan i) * (inf + nan i) = "
print $ (inf :+ nan) * (inf :+ nan)
putStr "1 / (inf + nan i) = "
print $ 1 / (inf :+ nan)
putStr "1 / (0 + 0 i) = "
print $ 1 / (0 :+ 0)
putStr "(inf + nan i) / (1 + 2 i) = "
print $ (inf :+ nan) / (1 :+ 2)
open Complex;;
open Printf;;
let print_complex z = printf "%g + %g i" z.re z.im;;
print_endline "OCaml Complex";
print_string "abs(1e300 + 1e300 i) = ";
print_float (Complex.norm {re=1e300;im=1e300});
print_newline ();
print_string "1 / (1e300 + 1e300 i) = ";
print_complex (Complex.inv {re=1e300;im=1e300});
print_newline ();
print_string "hypot(inf, nan) = ";
print_float (hypot infinity nan);
print_newline ();
print_string "abs(inf + nan i) = ";
print_float (Complex.norm {re=infinity;im=nan});
print_newline ();
print_string "(1 + 1 i) * (inf + nan i) = ";
print_complex (Complex.mul {re=1.0;im=1.0} {re=infinity;im=nan});
print_newline ();
print_string "(inf + nan i) * (inf + nan i) = ";
print_complex (Complex.mul {re=infinity;im=nan} {re=infinity;im=nan});
print_newline ();
print_string "1 / (inf + nan i) = ";
print_complex (Complex.div {re=1.0;im=0.0} {re=infinity;im=nan});
print_newline ();
print_string "1 / (0 + 0 i) = ";
print_complex (Complex.div {re=1.0;im=0.0} {re=0.0;im=0.0});
print_newline ();
print_string "(inf + nan i) / (1 + 2 i) = ";
print_complex (Complex.div {re=infinity;im=nan} {re=1.0;im=2.0});
print_newline ();
from math import inf, nan
print("Python")
print("abs(1e300 + 1e300 i) =", abs(1e300 + 1e300j))
print("1 / (1e300 + 1e300 i) =", 1 / (1e300 + 1e300j))
print("abs(inf + nan i) =", abs(complex(inf, nan)))
print("(1 + 1 i) * (inf + nan i) =", (1 + 1j) * complex(inf, nan))
print("(inf + nan i) * (inf + nan i) =", complex(inf, nan) * complex(inf, nan))
print("1 / (inf + nan i) =", 1 * complex(inf, nan))
# print(1 / complex(0, 0)) --> ZeroDivisionError: complex division by zero
print("(inf + nan i) / (1 + 2 i) =", complex(inf, nan) / complex(1, 2))
puts "Ruby"
print "abs(1e300 + 1e300 i) = "
puts(Complex(1e300, 1e300).abs)
print "1 / (1e300 + 1e300 i) = "
puts(1 / Complex(1e300, 1e300))
print "abs(inf + nan i) = "
puts(Complex(Float::INFINITY, Float::NAN).abs)
print "(1 + 1 i) * (inf + nan i) = "
puts(Complex(1, 1) * Complex(Float::INFINITY, Float::NAN))
print "(inf + nan i) * (inf + nan i) = "
puts(Complex(Float::INFINITY, Float::NAN) * Complex(Float::INFINITY, Float::NAN))
print "1 / (inf + nan i) = "
puts(1 / Complex(Float::INFINITY, Float::NAN))
# puts(1 / Complex(0, 0)) --> divided by 0 (ZeroDivisionError)
print "(inf + nan i) / (1 + 2 i) = "
puts(Complex(Float::INFINITY, Float::NAN) / Complex(1, 2))
extern crate num;
use num::Complex;
fn main() {
println!("Rust num::complex");
println!("abs(1e300 + 1e300 i) = {:e}", Complex::new(1e300, 1e300).norm());
{
let z = 1f64 / Complex::new(1e300, 1e300);
println!("1 / (1e300 + 1e300 i) = {:e} + {:e} i", z.re, z.im);
}
let inf = std::f64::INFINITY;
let nan = std::f64::NAN;
println!("abs(inf + nan i) = {:e}", Complex::new(inf, nan).norm());
{
let z = Complex::new(1.0, 1.0) * Complex::new(inf, nan);
println!("(1 + 1 i) * (inf + nan i) = {:e} + {:e} i", z.re, z.im);
}
{
let z = Complex::new(inf, nan) * Complex::new(inf, nan);
println!("(inf + nan i) * (inf + nan i) = {:e} + {:e} i", z.re, z.im);
}
{
let z = 1.0 / Complex::new(inf, nan);
println!("1 / (inf + nan i) = {:e} + {:e} i", z.re, z.im);
}
{
let z = 1f64 / Complex::new(0.0, 0.0);
println!("1 / (0 + 0 i) = {:e} + {:e} i", z.re, z.im);
}
{
let z = Complex::new(inf, nan) / Complex::new(1.0, 2.0);
println!("(inf + nan i) / (1 + 2 i) = {:e} + {:e} i", z.re, z.im);
}
}
import std.stdio;
import std.math;
void main() {
writeln("D cdouble");
writeln("abs(1e300 + 1e300 i) = ", abs(1e300 + 1e300i));
writeln("1 / (1e300 + 1e300 i) = ", 1.0 / (1e300 + 1e300i));
writeln("abs(inf + nan i) = ", abs(real.infinity + real.nan * 1.0i));
writeln("(1 + 1 i) * (inf + nan i) = ", (1.0 + 1.0i) * (real.infinity + real.nan * 1.0i));
writeln("(inf + nan i) * (inf + nan i) = ", (real.infinity + real.nan * 1.0i) * (real.infinity + real.nan * 1.0i));
writeln("1 / (inf + nan i) = ", 1.0 / (real.infinity + real.nan * 1.0i));
writeln("1 / (0 + 0 i) = ", 1.0 / (0.0 + 0.0i));
writeln("(inf + nan i) / (1 + 2 i) = ", (real.infinity + real.nan * 1.0i) / (1.0 + 2.0i));
}
import std.stdio;
import std.math;
import std.complex;
void main() {
writeln("D std.complex");
writeln("abs(1e300 + 1e300 i) = ", abs(complex(1e300, 1e300)));
writeln("1 / (1e300 + 1e300 i) = ", 1.0 / complex(1e300, 1e300));
writeln("abs(inf + nan i) = ", abs(complex(real.infinity, real.nan)));
writeln("(1 + 1 i) * (inf + nan i) = ", complex(1.0, 1.0) * complex(real.infinity, real.nan));
writeln("(inf + nan i) * (inf + nan i) = ", complex(real.infinity, real.nan) * complex(real.infinity, real.nan));
writeln("1 / (inf + nan i) = ", 1.0 / complex(real.infinity, real.nan));
writeln("1 / (0 + 0 i) = ", 1.0 / complex(0.0, 0.0));
writeln("(inf + nan i) / (1 + 2 i) = ", complex(real.infinity, real.nan) / complex(1.0, 2.0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment