π Day puzzle for 3/14

C + x86 assembly

Not satisfied with a constant defined in the software of your language? Why not use a language that can access a constant value of PI right from your FPU hardware:

#include <stdio.h>

int main (int argc, char **argv) {
    double pi;
    __asm__("fldpi" : "=t" (pi));
    printf("%g\n", 3 * 3 * pi);
    return (0);
}

Python, bash, C, J, PHP and Python3

import subprocess

p = subprocess.Popen("""
echo '
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>

int main(int pi) {
    if (pi == 1) printf("%.5f", M_PI);
    if (pi == 2) printf("o. 1");
    if (pi == 3) printf("<?php printf(\\"%%.5f\\", pi()); ?>");
    if (pi == 4) printf("import math; print(\\" %%.5f\\" %% math.pi)");
    return 0;
}
' > gcc -o pi
./pi
./pi J | jc
./pi and PHP | php
./pi and Python 3 | python3
""", shell=True, stdout=subprocess.PIPE)

values_of_pi = map(float, map(str.strip, p.stdout.read().split()))
pi = max(values_of_pi, key=values_of_pi.count)

print pi * 3 * 3

Just to be safe, this program retrieves pi from a few different languages, taking the most agreed upon value. More languages can easily be added for greater reliability.


PHP/MYSQL

$link = mysqli_connect("localhost", "user", "password", "dbname");
$query = mysqli_query($link, 'SELECT PI() AS pi');
$row = mysqli_fetch_assoc($query);
echo 3*3*$row['pi'];