Leibniz

Computing π with the Leibniz formula

benchmarks/leibniz.bench.tal
//!
//! Example: "benchmarks/leibniz.bench.tal"
//! Description: Traces different "leibniz" benchmarks.
//!

// Talos Modules
import "talos:test" as Test;

// - TEST SETUP - //

export let leibniz = fn (iterations: Number): Number {
    mut pi = 1; // prepare output
    mut sign = 1; // and the sign

    for (ii in List.range(2, iterations)) {
        sign = -sign; // update
        pi += sign / (2 * ii - 1);
    }

    // return the resulting pi-value
    return pi * 4;
};

// - TEST CASES - //

Test.bench("leibniz", fn => leibniz(1000000));

On this page