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
+50
@@ -0,0 +1,50 @@
|
||||
# Calculate arctan(1/x) using Taylor series with integer arithmetic
|
||||
def arctan(x, decimals)
|
||||
scale = 10 ** (decimals + 10)
|
||||
x_squared = x * x
|
||||
|
||||
result = 0
|
||||
term = scale / x
|
||||
|
||||
n = 0
|
||||
while term != 0 && n < decimals * 3
|
||||
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
|
||||
|
||||
# Calculate pi using Machin's formula
|
||||
def calculate_pi(decimals)
|
||||
atan1_5 = arctan(5, decimals)
|
||||
atan1_239 = arctan(239, decimals)
|
||||
|
||||
16 * atan1_5 - 4 * atan1_239
|
||||
end
|
||||
|
||||
# Format pi with decimal point
|
||||
def format_pi(pi, decimals)
|
||||
pi_str = pi.to_s
|
||||
# Pad with leading zeros if needed
|
||||
while pi_str.length < decimals + 10
|
||||
pi_str = '0' + pi_str
|
||||
end
|
||||
"3.#{pi_str[1, decimals]}"
|
||||
end
|
||||
|
||||
# Main
|
||||
decimals = ARGV[0]&.to_i || 100
|
||||
decimals = 100 if decimals <= 0
|
||||
|
||||
pi = calculate_pi(decimals)
|
||||
print format_pi(pi, decimals)
|
||||
@@ -0,0 +1,49 @@
|
||||
require 'test/unit'
|
||||
require 'open3'
|
||||
|
||||
class PiTest < Test::Unit::TestCase
|
||||
SCRIPT_PATH = '/Users/einand/Code/test/ruby/print_hej.rb'
|
||||
|
||||
def run_script(*args)
|
||||
cmd = ['ruby', SCRIPT_PATH] + args.map(&:to_s)
|
||||
stdout, _ = Open3.capture2(*cmd)
|
||||
stdout.strip
|
||||
end
|
||||
|
||||
def test_10_decimals
|
||||
result = run_script(10)
|
||||
expected = '3.1415926535'
|
||||
assert_equal expected, result
|
||||
end
|
||||
|
||||
def test_5_decimals
|
||||
result = run_script(5)
|
||||
expected = '3.14159'
|
||||
assert_equal expected, result
|
||||
end
|
||||
|
||||
def test_1_decimal
|
||||
result = run_script(1)
|
||||
expected = '3.1'
|
||||
assert_equal expected, result
|
||||
end
|
||||
|
||||
def test_100_decimals
|
||||
result = run_script(100)
|
||||
expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679'
|
||||
assert_equal expected, result
|
||||
end
|
||||
|
||||
def test_default_100_decimals
|
||||
result = run_script
|
||||
expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679'
|
||||
assert_equal expected, result
|
||||
end
|
||||
|
||||
def test_10000_decimals
|
||||
result = run_script(10000)
|
||||
# Check length: "3." + 10000 digits = 10002 characters
|
||||
assert_equal 10002, result.length
|
||||
assert result.start_with?('3.14159')
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user