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:
Executable
+28
@@ -0,0 +1,28 @@
|
||||
#!/bin/bash
|
||||
|
||||
# TypeScript Build Script - TypeScript är interpreterat, ingen kompilering behövs
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
echo "=== TypeScript Build ==="
|
||||
echo "TypeScript är ett interpreterat språk, ingen kompilering behövs."
|
||||
echo "Script: src/print_hej.ts"
|
||||
echo ""
|
||||
|
||||
# Skapa wrapper script
|
||||
mkdir -p bin
|
||||
cat > bin/print_hej << 'EOF'
|
||||
#!/bin/bash
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
npx ts-node src/print_hej.ts "$@"
|
||||
EOF
|
||||
chmod +x bin/print_hej
|
||||
|
||||
echo "Wrapper script skapad: bin/print_hej"
|
||||
echo ""
|
||||
echo "För att köra:"
|
||||
echo " ./bin/print_hej [decimaler]"
|
||||
echo ""
|
||||
echo "✓ Klart!"
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
# TypeScript Unit Tests
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
echo "=== TypeScript Pi-beräkning Unit Tester (Jest) ==="
|
||||
echo ""
|
||||
|
||||
cd src
|
||||
npm test
|
||||
|
||||
exit $?
|
||||
Generated
+3880
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "typescript-pi",
|
||||
"version": "1.0.0",
|
||||
"description": "TypeScript pi calculation",
|
||||
"scripts": {
|
||||
"test": "jest"
|
||||
},
|
||||
"dependencies": {
|
||||
"big.js": "^7.0.1",
|
||||
"typescript": "^6.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/big.js": "^6.2.2",
|
||||
"@types/node": "^25.6.0",
|
||||
"jest": "^29.0.0",
|
||||
"ts-jest": "^29.0.0",
|
||||
"@types/jest": "^29.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import Big from 'big.js';
|
||||
|
||||
// Calculate arctan(1/x) using Taylor series
|
||||
function arctan(x: number, decimals: number): Big {
|
||||
const scale = new Big(10).pow(decimals + 10);
|
||||
const xSquared = x * x;
|
||||
|
||||
let term = scale.div(x);
|
||||
let result = new Big(0);
|
||||
let n = 0;
|
||||
|
||||
while (term.gt(0)) {
|
||||
const divisor = 2 * n + 1;
|
||||
const contrib = term.div(divisor);
|
||||
|
||||
if (n % 2 === 0) {
|
||||
result = result.plus(contrib);
|
||||
} else {
|
||||
result = result.minus(contrib);
|
||||
}
|
||||
|
||||
term = term.div(xSquared);
|
||||
n++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Calculate pi using Machin's formula
|
||||
function calculatePi(decimals: number): string {
|
||||
const atan1_5 = arctan(5, decimals);
|
||||
const atan1_239 = arctan(239, decimals);
|
||||
|
||||
// pi/4 = 4*arctan(1/5) - arctan(1/239)
|
||||
// pi = 16*arctan(1/5) - 4*arctan(1/239)
|
||||
const pi = atan1_5.times(16).minus(atan1_239.times(4));
|
||||
|
||||
// Format with decimal point
|
||||
const piStr = pi.toFixed(decimals + 10);
|
||||
const digits = piStr.substring(1); // Skip the first digit (3)
|
||||
|
||||
let result = '3.';
|
||||
for (let i = 0; i < decimals; i++) {
|
||||
if (i < digits.length) {
|
||||
result += digits[i];
|
||||
} else {
|
||||
result += '0';
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Main
|
||||
const decimals = process.argv.length > 2 ? parseInt(process.argv[2]) || 100 : 100;
|
||||
console.log(calculatePi(decimals));
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Unit tests for TypeScript pi calculation using Jest
|
||||
*/
|
||||
|
||||
import { execSync } from 'child_process';
|
||||
import * as path from 'path';
|
||||
|
||||
const SCRIPT_PATH = path.join(__dirname, 'print_hej.ts');
|
||||
|
||||
function runScript(decimals: number | null = null): string {
|
||||
const cmd = decimals ? `npx ts-node ${SCRIPT_PATH} ${decimals}` : `npx ts-node ${SCRIPT_PATH}`;
|
||||
return execSync(cmd, { encoding: 'utf8' }).trim();
|
||||
}
|
||||
|
||||
describe('Pi Calculation', () => {
|
||||
test('10 decimals', () => {
|
||||
const result = runScript(10);
|
||||
const expected = '3.1415926535';
|
||||
expect(result).toBe(expected);
|
||||
});
|
||||
|
||||
test('5 decimals', () => {
|
||||
const result = runScript(5);
|
||||
const expected = '3.14159';
|
||||
expect(result).toBe(expected);
|
||||
});
|
||||
|
||||
test('1 decimal', () => {
|
||||
const result = runScript(1);
|
||||
const expected = '3.1';
|
||||
expect(result).toBe(expected);
|
||||
});
|
||||
|
||||
test('100 decimals', () => {
|
||||
const result = runScript(100);
|
||||
const expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
|
||||
expect(result).toBe(expected);
|
||||
});
|
||||
|
||||
test('default (100 decimals)', () => {
|
||||
const result = runScript();
|
||||
const expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
|
||||
expect(result).toBe(expected);
|
||||
});
|
||||
|
||||
test('invalid input (should use default 100)', () => {
|
||||
const result = runScript(NaN);
|
||||
const expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
|
||||
expect(result).toBe(expected);
|
||||
});
|
||||
|
||||
test('10000 decimals', () => {
|
||||
const result = runScript(10000);
|
||||
// Check length: "3." + 10000 digits = 10002 characters
|
||||
expect(result.length).toBe(10002);
|
||||
expect(result.startsWith('3.14159')).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"module": "commonjs",
|
||||
"lib": ["ES2020"],
|
||||
"esModuleInterop": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"types": ["jest", "node"]
|
||||
},
|
||||
"include": ["*.ts"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user