Open ghost opened 1 year ago
Here is a BooBoo version with some improvements that reads the number of rounds from a file.
number f
file_open f "rounds.txt" "r"
string s
file_read f s
file_close f
number rounds
= rounds (+ 3 (* s 2))
number x
= x 1
number pi
= pi 1
number i
= i 3
:next_loop
neg x
+ pi (/ x i)
+ i 2
? i rounds
jl next_loop
* pi 4
print "%\n" pi
EDIT: updated it to make it a little bit faster. Perhaps the other benchmarks would benefit.
The only issue now is text formatting, it simply uses %g so it doesn't print all the digits it calculated.
I've added formatting options so it's fully functional.
number f
file_open f "rounds.txt" "r"
string s
file_read f s
file_close f
number rounds
= rounds (+ 3 (* s 2))
number x
= x 1
number pi
= pi 1
number i
= i 3
:next_loop
neg x
+ pi (/ x i)
+ i 2
? i rounds
jl next_loop
* pi 4
print "%(3.16g)\n" pi
It's a bit slower than Python but faster than ruby or perl for me.
I added for loops to BooBoo and that cut the overhead significantly, it is now quite a bit faster than Python but still slower than Lua.
number f
file_open f "rounds.txt" "r"
string s
file_read f s
file_close f
number rounds
= rounds (+ 3 (* s 2))
number x
= x 1
number pi
= pi 1
number i
= i 3
for i rounds 2 loop_end
neg x
+ pi (/ x i)
:loop_end
* pi 4
print "%(3.16g)\n" pi