Perl/Tk with C, Pascal, Java, JavaScript, LaTeX3, Prolog, Perl, Scheme, Lua, Python, TeX/PGF
The following Perl script displays a windows that lists the values of π and the calculated area. The value of π is taken from different languages
as shown below.
The one-file script:
#!/usr/bin/env perl
use strict;
$^W=1;
use Tk;
use Tk::Font;
use Tk::HList;
use Tk::ItemStyle;
use Tk::PNG;
# Function to calculate the area of the circle with radius 3
sub A ($) {
use bignum;
return 9*$_[0];
}
my $title = 'Pi Day';
# Configuration of external program names
my %prg = qw[
Pascal fpc
Perl perl
Prolog swipl
Scheme guile1
TeX tex
LaTeX latex
];
sub prg ($) {
my $prg = shift;
return $prg{$prg} // $prg;
}
# Column headers
my @header = (
'',
'Language',
"\N{U+03C0}",
"A(3) = A(r) = \N{U+03C0}\N{U+2009}r\N{U+00B2}",
);
my $mw = MainWindow->new(
-title => $title,
);
# Font setup (larger font)
my $font_size = '22';
my $font = $mw->Font();
$font->configure(-size => $font_size);
# ---------
# Utilities
# ---------
# Run program in backticks, quote arguments if needed and some error checking
sub backticks_pi (@) {
my @cmd = map{/[ ()$;<>|\x22]/ && length > 1 ? "'$_'" : $_} @_;
print "[@cmd]\n";
my $catch = `@cmd`;
if ($? == -1) {
warn "Failed to execute: $!\n";
}
elsif ($? & 127) {
warn sprintf "Child died with signal %d!\n", $? & 127;
}
elsif ($?) {
warn sprintf "Child exited with value %d!\n", $? >> 8;
}
else {
return $1 if $catch =~ /^\s*(\d+\.\d+)\s*$/
or $catch =~ /\bpi\s*=\s*(\d+\.\d+)/;
}
warn "Could not find pi in the output of \"@cmd\"!\n";
return 0;
}
# Run a program with error checking
sub run_cmd (@) {
print "[@_]\n";
system @_;
if ($? == -1) {
warn "Failed to execute: $!\n";
}
elsif ($? & 127) {
warn sprintf "Child died with signal %d!\n", $? & 127;
}
elsif ($?) {
warn sprintf "Child exited with value %d!\n", $? >> 8;
}
else {
return $1;
}
return undef;
}
# Create a bitmap logo
sub make_logo ($$$@) {
my $name = shift;
my $logo = shift;
my $contents = shift;
my $file = "piday-logo-$name.tmp";
if ($contents) {
open(OUT, '>', $file) or die "!!! Error: Cannot write `$file': $!";
print OUT $contents;
close(OUT);
}
foreach (@_) {
run_cmd @$_;
}
return $mw->Photo(
-file => $logo,
) if -f $logo;
return undef;
}
# Call foreign language to calculate pi
sub make_pi ($$@) {
my $file = shift;
my $source = shift;
if ($source) {
open(OUT, '>', $file) or die "!!! Error: Cannot write `$file': $!";
print OUT $source;
close(OUT);
}
my $cmd_last = pop;
foreach (@_) {
run_cmd @$_;
}
return backticks_pi @$cmd_last;
}
# Add result list table
my $h = $mw->HList(
-header => 1,
-columns => scalar @header,
-width => 100,
-height => 20,
-font => $font,
)->pack(
-expand => 1,
-fill => 'both',
);
# Add header for the result list table
for (0 .. @header-1) {
$h->header('create', $_,
-text => $header[$_],
);
}
# Exit button
my $quit = $mw->Button(
-text => 'Quit',
-command => sub {exit},
-font => $font,
)->pack;
my @list;
my @cmd;
my $pi;
my $source;
my $img;
# GNU C
# -----
$img = make_logo(
'C',
'piday-logo-c.png',
'',
[
prg('wget'),
'-O', 'piday-logo-c-gccegg.png',
'http://gcc.gnu.org/img/gccegg-65.png',
],
[
prg('convert'),
'-scale', '54x64',
'piday-logo-c-gccegg.png',
'piday-logo-c.png',
],
);
$source = <<'END_SOURCE';
#define _GNU_SOURCE
#include <math.h>
#include <stdio.h>
#define xstr(s) str(s)
#define str(s) #s
int main() {
long double pi = M_PI;
printf("pi=%s", xstr(M_PIl));
return 0;
}
END_SOURCE
$pi = make_pi(
'piday-c.c',
$source,
[
prg('gcc'),
'-o', 'piday-c',
'piday-c.c',
],
[
prg('./piday-c')
],
);
push @list, {
language => 'GNU C',
pi => $pi,
image => $img,
};
# Java
# ----
$img = make_logo(
'Java',
'piday-java.png',
'',
[
prg('wget'),
'-O', 'piday-java.svg',
'https://upload.wikimedia.org/wikipedia/commons/a/a4/Java_logo_and_wordmark.svg',
],
[
prg('convert'),
'-scale', '35x64',
'piday-java.svg',
'piday-java.png',
],
);
$source = <<'END_SOURCE';
public class PiDayJava {
public static void main(String args[]) {
System.out.println(Math.PI);
}
}
END_SOURCE
$pi = make_pi(
'PiDayJava.java',
$source,
[
prg('javac'),
'PiDayJava.java',
],
[
prg('java'),
'PiDayJava',
],
);
push @list, {
language => 'Java',
pi => $pi,
image => $img,
};
# Perl
# ----
# Math/Complex.pm: sub pi () { 4 * CORE::atan2(1, 1) }
@cmd = (prg('Perl'), '-e', 'use Math::Complex; print pi');
$pi = backticks_pi @cmd;
my $img = Tk->findINC('Camel.xpm');
$img = $mw->Photo(
-file => $img,
);
push @list, {
language => 'Perl',
pi => $pi,
image => $img,
};
# Python
# ------
@cmd = (
prg('echo'),
'import math;print math.pi',
'|',
prg('python'),
);
$pi = backticks_pi @cmd;
$img = make_logo(
'python',
'piday-logo-python.png',
'',
[
prg('wget'),
'-O',
'piday-logo-python-master.png',
'http://www.python.org/static/community_logos/python-logo-master-v3-TM.png',
],
[
prg('convert'),
'-crop', '111x111+79+33',
'piday-logo-python-master.png',
'piday-logo-python-crop.png'
],
[
prg('convert'),
'-scale', '64x64',
'piday-logo-python-crop.png',
'piday-logo-python.png',
],
);
push @list, {
language => 'Python',
pi => $pi,
image => $img,
};
# TeX
# ---
@cmd = (
prg('TeX'),
'\input pgf \pgfmathparse{pi}\message{pi=\pgfmathresult}\end',
);
$pi = backticks_pi @cmd;
my $img = make_logo(
'tex',
'piday-logo-tex.png',
'',
[
prg('pdftex'),
'\mag=4000 \nopagenumbers\font\sc=cmcsc10 \sc pgf\bye'
],
[
prg('pdfcrop'),
'texput.pdf',
'piday-logo-tex.pdf',
],
[
prg('convert'),
'piday-logo-tex.pdf',
'piday-logo-tex.png',
]
);
push @list, {
language => 'TeX/PGF',
pi => $pi,
image => $img,
};
# LaTeX3
# ------
my $logo_source = <<'END_LOGO';
\mag=4000
\documentclass{article}
\usepackage{hologo}
\pagestyle{empty}
\begin{document}
\hologo{LaTeX3}
\end{document}
END_LOGO
$img = make_logo(
'latex3',
'piday-logo-latex3.png',
$logo_source,
[
prg('pdflatex'),
'piday-logo-latex3.tmp'
],
[
prg('pdfcrop'),
'piday-logo-latex3.pdf',
'piday-logo-latex3-crop.pdf',
],
[
prg('convert'),
'piday-logo-latex3-crop.pdf',
'piday-logo-latex3.png',
]
);
$source = <<'END_LATEX3';
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\msg_term:n { pi=\fp_eval:n { pi } }
\ExplSyntaxOff
\stop
END_LATEX3
$pi = make_pi(
'piday-latex3.tex',
$source,
[
prg('LaTeX'),
'piday-latex3.tex',
],
);
push @list, {
language => 'LaTeX3',
pi => $pi,
image => $img,
};
print "****************\n";
# Lua
# ---
$img = make_logo(
'Lua',
'piday-logo-lua.png',
'',
[
prg('wget'),
'-O', 'piday-logo-lua.gif',
'http://www.lua.org/images/lua-logo.gif',
],
[
prg('convert'),
'-scale', '64x64', # '50x50',
'piday-logo-lua.gif',
'piday-logo-lua.png',
],
);
$source = 'print(math.pi)';
$pi = make_pi(
'piday-lua.lua',
$source,
[
prg('texlua'),
'piday-lua.lua',
]
);
push @list, {
language => 'Lua',
pi => $pi,
image => $img,
};
# JavaScript
# ----------
$img = make_logo(
'JavaScript',
'piday-logo-javascript.png',
'',
[
prg('wget'),
'-O', 'piday-logo-rhino.jpg',
'https://developer.mozilla.org/@api/deki/files/832/=Rhino.jpg',
],
[
prg('convert'),
'-scale', '127x64',
'piday-logo-rhino.jpg',
'piday-logo-javascript.png',
],
);
$source = 'print(Math.PI)';
$pi = backticks_pi(
prg('java'),
'-cp', prg('js.jar'),
'org.mozilla.javascript.tools.shell.Main',
'-e', $source,
);
push @list, {
language => 'JavaScript',
pi => $pi,
image => $img,
};
# Scheme
# ------
$img = make_logo(
'Scheme',
'piday-logo-scheme.png',
'',
[
prg('wget'),
'-O', 'piday-logo-lambda.svg',
'https://upload.wikimedia.org/wikipedia/commons/3/39/Lambda_lc.svg',
],
[
prg('convert'),
'-scale', '64x64',
'piday-logo-lambda.svg',
'piday-logo-scheme.png',
],
);
$source = '(display (* 2 (acos 0)))';
$pi = backticks_pi(
prg('Scheme'),
'-c', $source,
);
push @list, {
language => 'Scheme',
pi => $pi,
image => $img,
};
# Prolog
# ------
$img = make_logo(
'Prolog',
'piday-logo-prolog.png',
'',
[
prg('wget'),
'-O', 'piday-logo-swipl.png',
'http://www.swi-prolog.org/icons/swipl.png',
],
[
prg('convert'),
'-scale', '78x64',
'piday-logo-swipl.png',
'piday-logo-prolog.png',
],
);
$source = ":- format('~15f~n', [pi]).\n";
$pi = make_pi(
'piday-prolog.pro',
$source,
[
prg('Prolog'),
'-c', 'piday-prolog.pro',
]
);
push @list, {
language => 'Prolog',
pi => $pi,
image => $img,
};
# Pascal
# ------
$img = make_logo(
'Pascal',
'piday-logo-pascal.gif',
'',
[
prg('wget'),
'-O', 'piday-logo-pascal.gif',
'http://www.freepascal.org/pic/logo.gif',
]
);
$source = <<'END_PASCAL';
program piday_pascal;
uses sysutils, math;
begin
writeln(format('%.16f', [pi]));
end.
END_PASCAL
$pi = make_pi(
'piday-pascal.pas',
$source,
[
prg('Pascal'),
'piday-pascal.pas',
],
[
prg('./piday-pascal'),
]
);
push @list, {
language => 'Pascal',
pi => $pi,
image => $img,
};
# Sort and fill the table rows
@list = sort {
my $diff = (length $b->{'pi'} <=> length $a->{'pi'});
return $diff if $diff;
return "\L$a->{'language'}\E" cmp "\L$b->{'language'}\E";
} @list;
foreach my $x (@list) {
my $e = $h->addchild("");
my $col = 0;
if ($x->{'image'}) {
$h->itemCreate($e, $col++,
-itemtype => 'image',
-image => $x->{'image'},
);
}
else {
$col++;
}
$h->itemCreate($e, $col++,
-itemtype => 'text',
-text => $x->{'language'},
);
$h->itemCreate($e, $col++,
-itemtype => 'text',
-text => $x->{'pi'},
);
$h->itemCreate($e, $col++,
-itemtype => 'text',
-text => A $x->{'pi'},
);
}
MainLoop;
__END__
Languages
The following list shows the languages and the code that is used to get π.
GNU C: GNU extensions are used to get a higher precision of π.
#define _GNU_SOURCE
#include <math.h>
#include <stdio.h>
#define xstr(s) str(s)
#define str(s) #s
int main() {
long double pi = M_PI;
printf("pi=%s", xstr(M_PIl));
return 0;
}
Pascal: Compiled with Free Pascal.
program piday_pascal;
uses sysutils, math;
begin
writeln(format('%.16f', [pi]));
end.
Java:
public class PiDayJava {
public static void main(String args[]) {
System.out.println(Math.PI);
}
}
JavaScript: Rhino is used for executing JavaScript.
print(Math.PI)
LaTeX3:
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\msg_term:n { pi=\fp_eval:n { pi } }
\ExplSyntaxOff
\stop
Prolog: SWI Prolog is used as Prolog compiler.
:- format('~15f~n', [pi]).
Perl: For fun and completeness.
use Math::Complex;
print pi;
Scheme: The uses Scheme implementation is GNU Guile.
(display (* 2 (acos 0)))
Lua: texlua
is used as Lua interpreter.
print(math.pi)
Python:
import math
print math.pi
TeX/PGF: π is taken from its definition of package pgf and plain TeX is used as TeX format:
\input pgf
\pgfmathparse{pi}
\message{pi=\pgfmathresult}
\end
2@TheDoctor Except, you didn't. – Martin Ender – 2015-03-14T20:18:48.050
38It's the 3rd day of the 14th month?? My calendar must be broken. – Gareth – 2014-03-14T17:03:57.200
You beat me to posting a Pi Day challenge :P – The Guy with The Hat – 2014-03-14T18:09:22.457
30Next year: 3/14/15 at 9:26 and 53sec, i will post a challenge – TheDoctor – 2014-03-14T22:32:27.537
5
Ugh. Tau is better. And this.
– bjb568 – 2014-03-15T01:54:44.2173@bjb I agree, Tau makes more sense, but that doesn't mean we can't have a little fun in mid march. :) – undergroundmonorail – 2014-03-15T03:40:37.080
2
@under Whada you mean? There's National Pig Day!
– bjb568 – 2014-03-15T04:08:21.630122/7 is a better pi day. – Ben Millwood – 2014-03-15T17:17:29.753
1I'm not sure what Pi day is, but this would have been excellent for Tau/2 day yesterday. – OJFord – 2014-03-15T21:08:27.037
9How intriguing. On π day, my reputation was 314. – tbodt – 2014-03-16T02:52:13.263
@tbodt How intriguing. On π day (and on all other days), a new user starts with a reputation of
314/314
. – devnull – 2014-03-17T06:42:24.6772http://xkcd.com/1292/ (seriously, stop this tau silliness). – Griwes – 2014-03-17T09:38:55.927
3Have anyone spotted, that this time it is not only pi day but a pi month? – V-X – 2014-03-18T07:26:59.087
I really dislike this whole "pi day" thing because it focuses on the complete wrong aspect of pi: its decimal radix form. Pi is important because it is the half period of exp(x*sqrt(-1)) WRT x, so pi day should be at the half periods of the tropical year, i.e. January 1st and July 2nd/3rd. – AJMansfield – 2014-03-18T15:38:20.800