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
+23
@@ -0,0 +1,23 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Crystal Build Script
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
echo "=== Crystal Build ==="
|
||||
echo ""
|
||||
|
||||
# Kompilera Crystal-programmet
|
||||
crystal build -o bin/print_hej src/print_hej.cr
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "✓ Kompilering lyckades!"
|
||||
echo "Binär: bin/print_hej"
|
||||
echo ""
|
||||
echo "För att köra:"
|
||||
echo " ./bin/print_hej [decimaler]"
|
||||
else
|
||||
echo "✗ Kompilering misslyckades!"
|
||||
exit 1
|
||||
fi
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Crystal Unit Tests
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
echo "=== Crystal Pi-beräkning Unit Tester ==="
|
||||
echo ""
|
||||
|
||||
cd src
|
||||
crystal spec
|
||||
|
||||
exit $?
|
||||
@@ -0,0 +1,63 @@
|
||||
require "big"
|
||||
|
||||
def pow10(exp : Int32) : BigInt
|
||||
result = BigInt.new(1)
|
||||
exp.times do
|
||||
result *= 10
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
def arctan(x : Int32, decimals : Int32) : BigInt
|
||||
scale = pow10(decimals + 10)
|
||||
x_squared = x * x
|
||||
|
||||
term = scale // x
|
||||
result = BigInt.new(0)
|
||||
n = 0
|
||||
|
||||
while term != 0
|
||||
divisor = 2 * n + 1
|
||||
contrib = term // divisor
|
||||
|
||||
if n % 2 == 0
|
||||
result += contrib
|
||||
else
|
||||
result -= contrib
|
||||
end
|
||||
|
||||
term = term // x_squared
|
||||
n += 1
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
def calculate_pi(decimals : Int32) : String
|
||||
atan1_5 = arctan(5, decimals)
|
||||
atan1_239 = arctan(239, decimals)
|
||||
|
||||
# pi = 16*arctan(1/5) - 4*arctan(1/239)
|
||||
pi = 16 * atan1_5 - 4 * atan1_239
|
||||
|
||||
pi_str = pi.to_s
|
||||
|
||||
# Format with decimal point
|
||||
result = "3."
|
||||
start = 1
|
||||
|
||||
decimals.times do |i|
|
||||
if start + i < pi_str.size
|
||||
result += pi_str[start + i].to_s
|
||||
else
|
||||
result += "0"
|
||||
end
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
# Main
|
||||
decimals = ARGV.size > 0 ? ARGV[0].to_i? || 100 : 100
|
||||
|
||||
puts calculate_pi(decimals)
|
||||
@@ -0,0 +1,50 @@
|
||||
require "spec"
|
||||
require "../print_hej"
|
||||
|
||||
SCRIPT_PATH = "/Users/einand/Code/test/crystal/print_hej"
|
||||
|
||||
def run_script(args = nil)
|
||||
cmd = [SCRIPT_PATH]
|
||||
cmd << args.to_s if args
|
||||
output = `#{cmd.join(" ")}`
|
||||
output.strip
|
||||
end
|
||||
|
||||
describe "Pi calculation" do
|
||||
it "calculates 10 decimals" do
|
||||
result = run_script(10)
|
||||
expected = "3.1415926535"
|
||||
result.should eq(expected)
|
||||
end
|
||||
|
||||
it "calculates 5 decimals" do
|
||||
result = run_script(5)
|
||||
expected = "3.14159"
|
||||
result.should eq(expected)
|
||||
end
|
||||
|
||||
it "calculates 1 decimal" do
|
||||
result = run_script(1)
|
||||
expected = "3.1"
|
||||
result.should eq(expected)
|
||||
end
|
||||
|
||||
it "calculates 100 decimals" do
|
||||
result = run_script(100)
|
||||
expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
|
||||
result.should eq(expected)
|
||||
end
|
||||
|
||||
it "calculates default 100 decimals" do
|
||||
result = run_script
|
||||
expected = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
|
||||
result.should eq(expected)
|
||||
end
|
||||
|
||||
it "calculates 10000 decimals" do
|
||||
result = run_script(10000)
|
||||
# Check length: "3." + 10000 digits = 10002 characters
|
||||
result.size.should eq(10002)
|
||||
result.starts_with?("3.14159").should be_true
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user