Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ collect-data:
BUILD +lua
BUILD +luajit
BUILD +nim
BUILD +ocaml
BUILD +php
BUILD +perl
BUILD +pony
Expand Down Expand Up @@ -303,6 +304,12 @@ nim:
RUN --no-cache nim c --verbosity:0 -d:danger -d:lto --gc:arc --passC:"-march=native -fno-signed-zeros -fno-trapping-math -fassociative-math" --passL:"-s" leibniz.nim
DO +BENCH --name="nim" --lang="Nim" --version="nim --version" --cmd="./leibniz"

ocaml:
FROM +alpine --src="leibniz.ml"
RUN apk add --no-cache ocaml
RUN --no-cache ocamlopt -O2 -o leibniz leibniz.ml
DO +BENCH --name="ocaml" --lang="OCaml" --version="ocamlopt -version" --cmd="./leibniz"

php:
FROM +alpine --src="leibniz.php"
RUN apk add --no-cache php81
Expand Down
19 changes: 19 additions & 0 deletions src/leibniz.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let calc rounds =
let sum = ref 0. in
let double = 2 * rounds in
let curr = ref (1 - double) in
let upto = double + 1 in
while !curr < upto do
sum := !sum +. 1. /. float_of_int !curr;
curr := !curr + 4
done;
!sum

let () =
let rounds_txt = open_in_bin "rounds.txt" in
let rounds = int_of_string (input_line rounds_txt) in
close_in rounds_txt;

let pi = 4. *. calc rounds in
Printf.printf "%.16f\n" pi