Skip to content

Commit

Permalink
Add leibniz formula to calculate pi
Browse files Browse the repository at this point in the history
  • Loading branch information
leewei05 committed Mar 4, 2024
1 parent fe9ebf1 commit 1ca227d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
40 changes: 40 additions & 0 deletions benchmarks/float/leibniz.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@main {
pi: float = const 0;
# denominator
denom: float = const 1;
is_even: bool = const false;
i: int = const 1;
end: int = const 1000000;

.for.start:
b: bool = lt i end;
br b .for.body .for.end;

.for.body:
fone: float = const 1;
br is_even .if.true .if.false;

.if.true:
f1: float = fdiv fone denom;
pi: float = fsub pi f1;
jmp .if.end;

.if.false:
f2: float = fdiv fone denom;
pi: float = fadd pi f2;

.if.end:
two: float = const 2;
denom: float= fadd denom two;

# step
is_even: bool = not is_even;
one: int = const 1;
i: int = add i one;
jmp .for.start;

.for.end:
four: float = const 4;
pi: float = fmul pi four;
print pi;
}
1 change: 1 addition & 0 deletions benchmarks/float/leibniz.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.14159365359077425
1 change: 1 addition & 0 deletions benchmarks/float/leibniz.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 12499997
5 changes: 3 additions & 2 deletions docs/tools/bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The current benchmarks are:
* `hanoi`: Print the solution to the *n*-disk [Tower of Hanoi][hanoi] puzzle.
* `is-decreasing`: Print if a number contains strictly decreasing digits.
* `lcm`: Compute LCM for two numbers using a very inefficient loop.
* `leibniz`: Approximates Pi using [Leibniz formula](https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80).
* `loopfact`: Compute *n!* imperatively using a loop.
* `major-elm`: Find the majority element in an array using [a linear time voting algorithm](https://www.cs.utexas.edu/~moore/best-ideas/mjrty/).
* `mandelbrot`: Generates a really low resolution, ascii, [mandelbrot set][mandelbrot].
Expand All @@ -46,7 +47,7 @@ The current benchmarks are:
* `max-subarray`: solution to the classic Maximum Subarray problem.
* `mod_inv`: Calculates the [modular inverse][modinv] of `n` under to a prime modulus p.
* `newton`: Calculate the square root of 99,999 using the [newton method][newton]
* `norm`: Calculate the [euclidean norm][euclidean] of a vector
* `norm`: Calculate the [euclidean norm][euclidean] of a vector
* `n_root`: Calculate nth root of a float using newton's method.
* `orders`: Compute the order ord(u) for each u in a cyclic group [<Zn,+>][cgroup] of integers modulo *n* under the group operation + (modulo *n*). Set the second argument *is_lcm* to true if you would like to compute the orders using the lowest common multiple and otherwise the program will use the greatest common divisor.
* `pascals-row`: Computes a row in Pascal's Triangle.
Expand All @@ -58,7 +59,7 @@ The current benchmarks are:
* `pythagorean_triple`: Prints all Pythagorean triples with the given c, if such triples exist. An intentionally very naive implementation.
* `quadratic`: The [quadratic formula][qf], including a hand-rolled implementation of square root.
* `quickselect`: Find the kth smallest element in an array using the quickselect algorithm.
* `quicksort`: [Quicksort using the Lomuto partition scheme][qsort].
* `quicksort`: [Quicksort using the Lomuto partition scheme][qsort].
* `quicksort-hoare`: Quicksort using [Hoare partioning][qsort-hoare] and median of three pivot selection.
* `recfact`: Compute *n!* using recursive function calls.
* `rectangles-area-difference`: Output the difference between the areas of rectangles (as a positive value) given their respective side lengths.
Expand Down

0 comments on commit 1ca227d

Please sign in to comment.