Initial commit: Pi calculation benchmark with 34 languages
- Added implementations for: bash, brainfuck, c, cpp, crystal, csharp, d, dart, elixir, erlang, fortran, go, haskell, java, javascript, julia, kotlin, objective-c, scala, typescript, lua, nim, odin, perl, php, python, r, ruby, rust, swift, zig, assembly, vimscript, wolfram - All implementations use Machin's formula: π/4 = 4*arctan(1/5) - arctan(1/239) - Build system with ./build.sh, test system with ./test.sh - Performance testing with ./run_all.sh - Comprehensive README.md explaining performance differences - Test framework verifies correctness against known π values
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PiTest extends TestCase
|
||||
{
|
||||
private const SCRIPT_PATH = '/Users/einand/Code/test/php/print_hej.php';
|
||||
|
||||
private function runScript(?string $decimals = null): string
|
||||
{
|
||||
$cmd = ['php', self::SCRIPT_PATH];
|
||||
if ($decimals !== null) {
|
||||
$cmd[] = $decimals;
|
||||
}
|
||||
|
||||
$output = [];
|
||||
exec(implode(' ', $cmd), $output);
|
||||
return trim(implode("\n", $output));
|
||||
}
|
||||
|
||||
public function test10Decimals(): void
|
||||
{
|
||||
$result = $this->runScript('10');
|
||||
$expected = '3.1415926535';
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function test5Decimals(): void
|
||||
{
|
||||
$result = $this->runScript('5');
|
||||
$expected = '3.14159';
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function test1Decimal(): void
|
||||
{
|
||||
$result = $this->runScript('1');
|
||||
$expected = '3.1';
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function test100Decimals(): void
|
||||
{
|
||||
$result = $this->runScript('100');
|
||||
$expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testDefault100Decimals(): void
|
||||
{
|
||||
$result = $this->runScript();
|
||||
$expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function test10000Decimals(): void
|
||||
{
|
||||
$result = $this->runScript('10000');
|
||||
// Check length: "3." + 10000 digits = 10002 characters
|
||||
$this->assertEquals(10002, strlen($result));
|
||||
$this->assertTrue(strpos($result, '3.14159') === 0);
|
||||
}
|
||||
}
|
||||
Executable
+54
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
// Calculate arctan(1/x) using Taylor series
|
||||
function arctan($x, $decimals) {
|
||||
$scale = bcpow('10', $decimals + 10);
|
||||
$xSquared = bcmul($x, $x);
|
||||
|
||||
$result = '0';
|
||||
$term = bcdiv($scale, $x);
|
||||
|
||||
$n = 0;
|
||||
while (bccomp($term, '0') != 0 && $n < $decimals * 3) {
|
||||
$divisor = (string)(2 * $n + 1);
|
||||
$contrib = bcdiv($term, $divisor);
|
||||
|
||||
if ($n % 2 == 0) {
|
||||
$result = bcadd($result, $contrib);
|
||||
} else {
|
||||
$result = bcsub($result, $contrib);
|
||||
}
|
||||
|
||||
$term = bcdiv($term, $xSquared);
|
||||
$n++;
|
||||
|
||||
if ($n > $decimals * 2) break;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Calculate pi using Machin's formula
|
||||
function calculate_pi($decimals) {
|
||||
$atan1_5 = arctan(5, $decimals);
|
||||
$atan1_239 = arctan(239, $decimals);
|
||||
|
||||
return bcsub(bcmul($atan1_5, '16'), bcmul($atan1_239, '4'));
|
||||
}
|
||||
|
||||
// Format pi with decimal point
|
||||
function format_pi($pi, $decimals) {
|
||||
$piStr = $pi;
|
||||
while (strlen($piStr) < $decimals + 10) {
|
||||
$piStr = '0' . $piStr;
|
||||
}
|
||||
return '3.' . substr($piStr, 1, $decimals);
|
||||
}
|
||||
|
||||
// Main
|
||||
$decimals = isset($argv[1]) ? (int)$argv[1] : 100;
|
||||
if ($decimals <= 0) $decimals = 100;
|
||||
|
||||
$pi = calculate_pi($decimals);
|
||||
echo format_pi($pi, $decimals) . "\n";
|
||||
?>
|
||||
Reference in New Issue
Block a user