Leibniz

Computing π with the Leibniz formula

benchmarks/leibniz.bench.sabre
///
/// @example "benchmarks/leibniz.bench.sabre"
/// @describe Traces different "leibniz" benchmarks.
///

// Sabre Modules
import "sabre: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 Iterator.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));
Last updated on March 01, 2026

On this page