t27.aiРусский

Phi as a ratio

You will learn

How phi_ratio.t27 computes a split for any width, and why the distance from 1 / phi does not shrink at every step.

phi_ratio.t27 sets PHI_RATIO_TARGET to 1 / phi, about 0.618, and its function phi_split works for any width: of the N - 1 bits after the sign, round((N - 1) / phi^2) go to the exponent and the rest to the mantissa. Its table verify_phi_split holds seven widths, and all seven match the family: GF4 splits 1 and 2, GF8 3 and 4, GF12 4 and 7, GF16 6 and 9, GF20 7 and 12, GF24 9 and 14, GF32 12 and 19. The distance from 1 / phi does not fall at every step: GF8 at 0.132 is further than GF4 at 0.118, and GF16 at 0.049 is further than GF12 at 0.047.

Try it

Run the tests of phi_ratio.t27 and find the one that checks GF16 at 6 and 9. Then compute 3 / 4 and 4 / 7 against 0.618 and say which is further.

Open the interactive lesson →

GoldenFloat 2: phi as a ratio
GoldenFloat 2: phi as a ratio ↗

Seven widths split by one rule, and how far each sits from 1 / phi, read from phi_ratio.t27. Lesson 2 of the GoldenFloat course.

specs/numeric/phi_ratio.t27

// SPDX-License-Identifier: Apache-2.0
// t27/specs/numeric/phi_ratio.t27
// φ-Ratio Proof — Derivation of GoldenFloat exp/mantissa split
// NUMERIC-STANDARD-001 — Agent 9 (P0)

module PhiRatio {
    // Import sacred constants
    use math::constants;
    use math::sacred_physics;

    // ═════════════════════════════════════════════════════════════════
    // 1. Golden Ratio Target for Float Formats
    // ═════════════════════════════════════════════════════════════════════════

    // The ideal exp/mantissa ratio for floating point formats
    // Derived from sacred physics: 1/φ ≈ 0.618
    const PHI_RATIO_TARGET : f64 = sacred_physics::PHI_INV;  // 0.618...

    // φ² = φ + 1 (golden ratio identity)
    // This gives us: 1/φ = φ - 1 ≈ 0.618
    const PHI_SQ : f64 = sacred_physics::PHI * sacred_physics::PHI;

    // ═════════════════════════════════════════════════════════════════
    // 2. φ-Split Formula — Derive optimal exp/mantissa bits
    // ═════════════════════════════════════════════════════════════════════════

    // For a floating point format with N bits total (including sign):
    //   bits = sign + exp + mant
    //   sign = 1 (always)
    //   available = N - 1 = exp + mant
    //
    // The φ-principle states: exp/mant = 1/φ
    //   exp = (available) / (φ + 1)
    //   mant = available - exp
    //
    // Since φ + 1 = φ², we have:
    //   exp = (N - 1) / φ²
    //   mant = N - 1 - exp

    struct PhiSplitResult {
        exp_bits   : u8,
        mant_bits  : u8,
        ratio      : f64,
        phi_dist   : f64,
    }

    fn phi_split(bits: u8) -> PhiSplitResult {
        const available = bits - 1;  // Exclude sign bit
        const phi_sq = sacred_physics::PHI * sacred_physics::PHI;

        // exp = round((N-1) / φ²)
        const exp_raw = (available as f64) / phi_sq;
        const exp_bits = round(exp_raw) as u8;

        // mant = N - 1 - exp
        const mant_bits = available - exp_bits;

        const ratio = (exp_bits as f64) / (mant_bits as f64);
        const phi_dist = abs(ratio - PHI_RATIO_TARGET);

        return PhiSplitResult{
            exp_bits = exp_bits,
            mant_bits = mant_bits,
            ratio = ratio,
            phi_dist = phi_dist,
        };
    }

    // ═════════════════════════════════════════════════════════════════
    // 3. Verify GoldenFloat Family against φ-Split
    // ═════════════════════════════════════════════════════════════════════════

    struct FormatComparison {
        name              : string,
        bits              : u8,
        actual_exp        : u8,
        actual_mant       : u8,
        phi_split_exp     : u8,
        phi_split_mant    : u8,
        matches_phi_split : bool,
        tradeoff_note     : string,
    }

    fn verify_phi_split() -> [7]FormatComparison {
        return [
            // GF4: φ-split gives exp=1, mant=2 → MATCH
            FormatComparison{
                name = "GF4",
                bits = 4,
                actual_exp = 1,
                actual_mant = 2,
                phi_split_exp = 1,
                phi_split_mant = 2,
                matches_phi_split = true,
                tradeoff_note = "Perfect φ-split match",
            },
            // GF8: φ-split gives exp=3, mant=4 → MATCH
            FormatComparison{
                name = "GF8",
                bits = 8,
                actual_exp = 3,
                actual_mant = 4,
                phi_split_exp = 3,
                phi_split_mant = 4,
                matches_phi_split = true,
                tradeoff_note = "Exact match: round(7/φ²)=3",
            },
            // GF12: φ-split gives exp=4, mant=7 → MATCH
            FormatComparison{
                name = "GF12",
                bits = 12,
                actual_exp = 4,
                actual_mant = 7,
                phi_split_exp = 4,
                phi_split_mant = 7,
                matches_phi_split = true,
                tradeoff_note = "Exact match: round(11/φ²)=4",
            },
            // GF16: φ-split gives exp=6, mant=9 → MATCH
            FormatComparison{
                name = "GF16",
                bits = 16,
                actual_exp = 6,
                actual_mant = 9,
                phi_split_exp = 6,
                phi_split_mant = 9,
                matches_phi_split = true,
                tradeoff_note = "PRIMARY FORMAT: exact match: round(15/φ²)=6",
            },
            // GF20: φ-split gives exp=7, mant=12 → MATCH
            FormatComparison{
                name = "GF20",
                bits = 20,
                actual_exp = 7,
                actual_mant = 12,
                phi_split_exp = 7,
                phi_split_mant = 12,
                matches_phi_split = true,
                tradeoff_note = "Exact match: round(19/φ²)=7",
            },
            // GF24: φ-split gives exp=9, mant=14 → MATCH
            // (This row said 6/17 and "no match"; phi_split(24) is 9/14, see
            // invariant phi_split_gf24_matches_round below.)
            FormatComparison{
                name = "GF24",
                bits = 24,
                actual_exp = 9,
                actual_mant = 14,
                phi_split_exp = 9,
                phi_split_mant = 14,
                matches_phi_split = true,
                tradeoff_note = "Exact match: round(23/φ²)=9",
            },
            // GF32: φ-split gives exp=12, mant=19 → MATCH
            // (This row said 8/23 and "no match"; phi_split(32) is 12/19.)
            FormatComparison{
                name = "GF32",
                bits = 32,
                actual_exp = 12,
                actual_mant = 19,
                phi_split_exp = 12,
                phi_split_mant = 19,
                matches_phi_split = true,
                tradeoff_note = "Exact match: round(31/φ²)=12",
            },
        ];
    }

    // ═════════════════════════════════════════════════════════════════
    // 4. Theoretical Proofs
    // ═════════════════════════════════════════════════════════════════════════

    // Proof that φ-split minimizes information loss
    // for a given bit budget under scale-invariant assumptions.

    fn golden_self_similarity_proof() -> string {
        // The golden ratio φ is defined by identity: φ² = φ + 1
        // Dividing both sides by φ² gives: 1 = 1/φ + 1/φ²
        //
        // Self-similarity constraint for bit allocation:
        //   The ratio e/m should equal ratio m/(e+m)
        //   This means: e/m = 1/(e/m + 1)
        //
        // Let r = e/m. Then: r = 1/(r + 1)
        // Solving: r² + r - 1 = 0
        //   r = (√5 - 1)/2 = 1/φ ≈ 0.618
        //
        // This is NOT an optimization problem (maximizing e×m gives r=1 by AM-GM).
        // It is a self-similarity constraint — a defining property of φ.
        return "φ is unique self-similar proportion: e/m = m/(e+m) → r = 1/φ";
    }

    // Theorem 2: Optimal Rounding
    // The function round((N-1)/φ²) gives integer closest to φ-proportion.

    fn optimal_rounding_proof() -> string {
        // For integer bit allocation, we must choose between floor and ceil.
        // The φ-proportion gives exp_ideal = (N-1)/φ² (real number), and
        // round() picks the integer nearest to it.
        //
        // (This used to claim that 1/φ MAXIMIZES dynamic_range * precision.
        // It does not: maximizing e*m gives r = 1 by AM-GM, as the proof
        // above says. 1/φ is a self-similarity constraint, not an optimum.)
        return "exp = round((N-1)/φ²) is the integer nearest the φ-proportion exp/mant = 1/φ";
    }

    // ═════════════════════════════════════════════════════════════════
    // 5. Connection to Sacred Physics
    // ═════════════════════════════════════════════════════════════════════════

    // The φ-ratio appears throughout sacred physics:
    // - Consciousness threshold C = φ⁻¹
    // - Specious present t = φ⁻² seconds
    // - Neural gamma band f_γ = φ³ * π / γ
    //
    // GoldenFloat formats inherit this sacred proportion.

    fn sacred_connection() -> string {
        // Shared VALUE only: sacred_physics::C_THRESHOLD is defined as PHI_INV.
        // No physical link is claimed here [Open conjecture].
        return "GoldenFloat exp/mant target = sacred_physics::PHI_INV, the same value as sacred_physics::C_THRESHOLD";
    }

    // ═════════════════════════════════════════════════════════════════
    // 6. Utility functions
    // ═════════════════════════════════════════════════════════════════════════

    fn compute_phi_distance(exp_bits: u8, mant_bits: u8) -> f64 {
        const ratio = (exp_bits as f64) / (mant_bits as f64);
        return abs(ratio - PHI_RATIO_TARGET);
    }

    fn is_phi_optimal(exp_bits: u8, mant_bits: u8, tolerance: f64) -> bool {
        return compute_phi_distance(exp_bits, mant_bits) < tolerance;
    }

    fn recommend_format(total_bits: u8) -> PhiSplitResult {
        return phi_split(total_bits);
    }

    // ═════════════════════════════════════════════════════════════════
    // 7. Round function (stub)
    // ═════════════════════════════════════════════════════════════════════════

    fn round(x: f64) -> f64 {
        // Round to nearest integer (round half away from zero)
        if (x < 0.0) {
            let xi = x as i64;
            let frac = x - (xi as f64);
            if (frac <= -0.5) {
                return (xi - 1) as f64;
            }
            return xi as f64;
        }
        let xi = x as i64;
        let frac = x - (xi as f64);
        if (frac >= 0.5) {
            return (xi + 1) as f64;
        }
        return xi as f64;
    }

    fn abs(x: f64) -> f64 {
        if (x < 0.0) {
            return -x;
        }
        return x;
    }

    fn pow(base: f64, exp: f64) -> f64 {
        // Power function with binary exponentiation for integer exponents
        // and logarithm approximation for fractional exponents
        if (base <= 0.0) {
            if (exp == 0.0) {
                return 1.0;  // 0^0 defined as 1 in this context
            }
            if (base == 0.0 && exp > 0.0) {
                return 0.0;
            }
            if (base < 0.0 && exp == floor(exp)) {
                // Negative base with integer exponent: handle via absolute value
                let exp_int = exp as i64;
                let result = pow(-base, exp);
                if (exp_int % 2 == 0) {
                    return result;
                }
                return -result;
            }
            return 0.0 / 0.0;  // NaN for negative base with non-integer exp
        }

        // Handle n = 0
        if (exp == 0.0) {
            return 1.0;
        }

        // Check if exponent is an integer
        let is_integer = exp == floor(exp);

        if is_integer {
            // Integer exponent: use binary exponentiation
            let exp_int = exp as i64;
            let mut result = 1.0;
            let mut base_acc = base;
            let mut e = exp_int;

            if e < 0 {
                e = -e;
                base_acc = 1.0 / base_acc;
            }

            while e > 0 {
                if e % 2 == 1 {
                    result = result * base_acc;
                }
                base_acc = base_acc * base_acc;
                e = e / 2;
            }

            return result;
        }

        // Fractional exponent: x^y = exp(y * ln(x))
        let ln_x = ln_approx(base);
        let result = exp_approx(exp * ln_x);

        return result;
    }

    // Natural logarithm approximation
    fn ln_approx(x: f64) -> f64 {
        if x <= 0.0 {
            return 0.0 / 0.0;  // NaN for non-positive
        }
        if x == 1.0 {
            return 0.0;
        }

        // Use series: ln(x) = 2 * ((x-1)/(x+1) + 1/3*((x-1)/(x+1))^3 + ...)
        // Sixty terms, not four: four left ln(4) off by about 3e-3, so
        // pow(4.0, 0.5) missed 2.0 by far more than the 1e-6 its test asks.
        let t = (x - 1.0) / (x + 1.0);
        let t2 = t * t;
        let mut power = t;
        let mut sum = 0.0;
        let mut k = 0;
        while k < 60 {
            sum = sum + power / ((2 * k + 1) as f64);
            power = power * t2;
            k = k + 1;
        }
        return 2.0 * sum;
    }

    // Exponential approximation
    fn exp_approx(x: f64) -> f64 {
        if x == 0.0 {
            return 1.0;
        }

        // Use Taylor series: e^x = 1 + x + x^2/2! + x^3/3! + ...
        let mut result = 1.0;
        let mut term = 1.0;
        let mut n = 1;

        // For better range, use x/2^k approach
        let mut exp_x = x;
        if x > 10.0 {
            let k = floor(x / 10.0) as i64;
            exp_x = x - (k as f64) * 10.0;
        } else if x < -10.0 {
            let k = floor(-x / 10.0) as i64;
            exp_x = x + (k as f64) * 10.0;
        }

        // Taylor series (10 terms)
        for i in 1..=10 {
            term = term * exp_x / (i as f64);
            result = result + term;
        }

        return result;
    }

    // Floor function
    fn floor(x: f64) -> f64 {
        let xi = x as i64;
        if x >= 0.0 || x == xi as f64 {
            return xi as f64;
        }
        return (xi - 1) as f64;
    }

    // ═══════════════════════════════════════════════════════════════════════════════════════════════════════
    // TDD-Inside-Spec: Tests and Invariants for φ-Ratio
    // ═══════════════════════════════════════════════════════════════════════════════════════════════════════

    test phi_split_for_gf4_perfect_match
        given bits = 4
        when result = phi_split(bits)
        // 1/2 against 1/φ: phi_dist = 0.118034 (GF8, 3/4, is wider: 0.131966).
        // (This asserted phi_dist < 0.01, which no 4-bit split can meet.)
        then result.exp_bits == 1 and result.mant_bits == 2 and result.phi_dist < 0.12

    test phi_split_for_gf16_primary_format
        given bits = 16
        when result = phi_split(bits)
        then result.exp_bits == 6 and result.mant_bits == 9 and result.phi_dist < 0.05

    test phi_split_for_gf32_near_optimal
        given bits = 32
        when result = phi_split(bits)
        then result.exp_bits == 12 and result.mant_bits == 19 and result.phi_dist < 0.02

    test phi_split_sum_constraint
        given bits = 16
        when result = phi_split(bits)
        then result.exp_bits + result.mant_bits == bits - 1

    test phi_ratio_target_equals_phi_inverse
        given target = PHI_RATIO_TARGET
        when inverse = sacred_physics::PHI_INV
        then abs(target - inverse) < 1e-15

    test phi_split_ratio_approximates_phi_inverse
        given bits = 16
        when result = phi_split(bits)
        and   ratio = result.exp_bits as f64 / result.mant_bits as f64
        then abs(ratio - PHI_RATIO_TARGET) < 0.05

    test phi_optimality_proof_derivative
        // phi_optimality_proof() never existed; the proof is this function.
        given proof = golden_self_similarity_proof()
        when contains_optimal = proof.contains("r = 1/φ")
        then contains_optimal == true

    test compute_phi_distance_for_gf16
        given exp = 6
        and   mant = 9
        // 6/9 = 0.6667 against 0.6180: distance 0.0486.
        // (This said > 0.1 behind a `;` the parser dropped with the clause.)
        when distance = compute_phi_distance(exp, mant)
        then distance > 0.04 and distance < 0.05

    test is_phi_optimal_tolerance_check
        // GF16's real split. (It was 4/11, distance 0.254, which is NOT
        // within 0.05; the test asserted the opposite.)
        given exp = 6
        and   mant = 9
        and   tolerance = 0.05
        when optimal = is_phi_optimal(exp, mant, tolerance)
        then optimal == true

    test verify_phi_split_all_formats_compared
        given comparisons = verify_phi_split()
        when gf4_matches = comparisons[0].matches_phi_split
        and   gf16_primary = comparisons[3].tradeoff_note.contains("PRIMARY")
        then gf4_matches == true and gf16_primary == true

    test sacred_connection_phi_ratio_equals_threshold
        given connection = sacred_connection()
        when has_threshold = connection.contains("C_THRESHOLD")
        and   has_phi_inverse = connection.contains("PHI_INV")
        then has_threshold == true and has_phi_inverse == true

    test phi_ratio_round_positive
        given result = round(3.7)
        then result == 4.0

    test phi_ratio_round_negative
        given result = round(-3.7)
        then result == -4.0

    test phi_ratio_round_half_up
        given result = round(3.5)
        then result == 4.0

    test phi_ratio_round_half_down
        given result = round(-3.5)
        then result == -4.0

    test phi_ratio_round_integer
        given result = round(5.0)
        then result == 5.0

    test phi_ratio_round_zero
        given result = round(0.0)
        then result == 0.0

    test phi_ratio_pow_zero_exponent_returns_one
        given result = pow(2.0, 0.0)
        then abs(result - 1.0) < 1e-15

    test phi_ratio_pow_one_exponent_returns_base
        given result = pow(5.0, 1.0)
        then abs(result - 5.0) < 1e-15

    test phi_ratio_pow_positive_integer_exponent
        given result = pow(2.0, 10.0)
        and expected = 1024.0
        then abs(result - expected) < 1e-10

    test phi_ratio_pow_negative_integer_exponent
        given result = pow(2.0, -3.0)
        and expected = 0.125
        then abs(result - expected) < 1e-10

    test phi_ratio_pow_fractional_exponent
        given result = pow(4.0, 0.5)
        and expected = 2.0
        then abs(result - expected) < 1e-6

    test phi_ratio_pow_phi_squared
        given result = pow(sacred_physics::PHI, 2.0)
        and expected = sacred_physics::PHI * sacred_physics::PHI
        then abs(result - expected) < 1e-10

    test phi_ratio_pow_zero_base_positive_exponent
        given result = pow(0.0, 5.0)
        then result == 0.0

    test phi_ratio_pow_one_base_any_exponent
        given result1 = pow(1.0, 10.0)
        and result2 = pow(1.0, -5.0)
        then abs(result1 - 1.0) < 1e-15 and abs(result2 - 1.0) < 1e-15

    test phi_ratio_ln_approx_of_one
        given result = ln_approx(1.0)
        then abs(result) < 1e-15

    test phi_ratio_ln_approx_of_e
        given e = 2.718281828459045
        and result = ln_approx(e)
        then abs(result - 1.0) < 0.01

    test phi_ratio_ln_approx_negative_returns_nan
        given result = ln_approx(-1.0)
        then result != result  // NaN check

    test phi_ratio_exp_approx_zero
        given result = exp_approx(0.0)
        then abs(result - 1.0) < 1e-15

    test phi_ratio_exp_approx_one
        given e = 2.718281828459045
        and result = exp_approx(1.0)
        then abs(result - e) < 0.01

    test phi_ratio_exp_approx_negative
        given result = exp_approx(-1.0)
        and expected = 1.0 / 2.718281828459045
        then abs(result - expected) < 0.01

    test phi_ratio_floor_positive
        given result = floor(3.7)
        then result == 3.0

    test phi_ratio_floor_negative
        given result = floor(-3.2)
        then result == -4.0

    test phi_ratio_floor_integer
        given result = floor(5.0)
        then result == 5.0

    test phi_ratio_floor_zero
        given result = floor(0.0)
        then result == 0.0

    invariant phi_round_returns_integer
        // For every x; checked at three, each lands on a whole number.
        assert round(3.7) == floor(round(3.7)) and round(-3.7) == floor(round(-3.7))
        and    round(0.2) == floor(round(0.2))

    invariant phi_round_half_away_from_zero
        assert round(2.5) == 3.0 and round(-2.5) == -3.0

    invariant phi_round_symmetric
        // For every x >= 0; checked at three, including a half.
        assert round(-0.4) == -round(0.4) and round(-2.5) == -round(2.5)
        and    round(-7.6) == -round(7.6)

    invariant phi_pow_zero_exponent_identity
        // For every positive x; checked at three points, one on each side of 1.
        given x1 = 0.5
        and x2 = 2.5
        and x3 = 7.0
        assert abs(pow(x1, 0.0) - 1.0) < 1e-15 and abs(pow(x2, 0.0) - 1.0) < 1e-15
        and    abs(pow(x3, 0.0) - 1.0) < 1e-15

    invariant phi_pow_one_exponent_identity
        // For every valid x; checked at three points.
        given x1 = 0.5
        and x2 = 2.5
        and x3 = 7.0
        assert abs(pow(x1, 1.0) - x1) < 1e-15 and abs(pow(x2, 1.0) - x2) < 1e-15
        and    abs(pow(x3, 1.0) - x3) < 1e-15

    invariant phi_pow_multiply_exponents
        given a = 2.0
        and b = 3.0
        assert abs(pow(pow(a, 2.0), b) - pow(a, 6.0)) < 1e-10

    invariant phi_ln_exp_inversion
        given x = 2.0
        and y = ln_approx(x)
        then abs(exp_approx(y) - x) < 0.01

    invariant phi_exp_ln_inversion
        given x = 1.5
        and y = exp_approx(x)
        then abs(ln_approx(y) - x) < 0.01

    invariant phi_floor_returns_integer
        // For every x; checked at three, each lands on a whole number.
        assert floor(3.7) == 3.0 and floor(-3.2) == -4.0 and floor(0.0) == 0.0

    invariant phi_floor_monotonic
        given x1 = 2.5
        and x2 = 3.5
        assert floor(x1) <= floor(x2)

    invariant phi_floor_zero_or_less
        // For every x; checked at three, on both sides of zero.
        assert floor(3.7) <= 3.7 and floor(-3.2) <= -3.2 and floor(5.0) <= 5.0

    invariant phi_split_sum_equals_available_bits
        // For every width; checked at three.
        assert phi_split(8).exp_bits + phi_split(8).mant_bits == 7
        and    phi_split(16).exp_bits + phi_split(16).mant_bits == 15
        and    phi_split(32).exp_bits + phi_split(32).mant_bits == 31

    invariant phi_ratio_target_is_phi_inverse
        assert PHI_RATIO_TARGET == sacred_physics::PHI_INV

    invariant phi_distance_non_negative
        // For every split; checked below, at and above the target.
        assert compute_phi_distance(1, 2) >= 0.0 and compute_phi_distance(6, 9) >= 0.0
        and    compute_phi_distance(9, 1) >= 0.0

    invariant phi_optimal_proof_valid
        assert golden_self_similarity_proof().contains("1/φ")

    invariant gf4_format_is_phi_optimal
        // GF4 is the coarsest rung: 1/2 against 1/φ, distance 0.118034.
        // (This asserted < 0.01; four bits cannot get closer than 0.118.)
        assert phi_split(4).phi_dist < 0.12

    invariant exp_bits_less_than_total
        // For every width; checked at three.
        assert phi_split(8).exp_bits < 8 and phi_split(16).exp_bits < 16 and phi_split(32).exp_bits < 32

    invariant mant_bits_less_than_total
        // For every width; checked at three.
        assert phi_split(8).mant_bits < 8 and phi_split(16).mant_bits < 16 and phi_split(32).mant_bits < 32

    invariant phi_split_round_matches_all_formats
        // CRITICAL: Verify that round((N-1)/φ²) matches ALL GF formats exactly
        assert phi_split(4).exp_bits == 1   // GF4:  round(3/φ²) = round(1.146) = 1

    invariant phi_split_gf8_matches_round
        assert phi_split(8).exp_bits == 3   // GF8:  round(7/φ²) = round(2.674) = 3

    invariant phi_split_gf12_matches_round
        assert phi_split(12).exp_bits == 4  // GF12: round(11/φ²) = round(4.202) = 4

    invariant phi_split_gf16_matches_round
        assert phi_split(16).exp_bits == 6  // GF16: round(15/φ²) = round(5.729) = 6

    invariant phi_split_gf20_matches_round
        assert phi_split(20).exp_bits == 7  // GF20: round(19/φ²) = round(7.257) = 7

    invariant phi_split_gf24_matches_round
        assert phi_split(24).exp_bits == 9  // GF24: round(23/φ²) = round(8.785) = 9

    invariant phi_split_gf32_matches_round
        assert phi_split(32).exp_bits == 12 // GF32: round(31/φ²) = round(11.841) = 12

    invariant phi_distance_bound_by_zero
        assert compute_phi_distance(0, 1) == abs(0.0 - PHI_RATIO_TARGET)

    bench phi_split_computation_time
        measure: nanoseconds to compute phi_split(32)
        target: < 100ns

    bench verify_phi_split_computation_time
        measure: nanoseconds to verify all 7 formats
        target: < 500ns

    bench compute_phi_distance_throughput
        measure: phi_distance computations per second
        target: > 1M computations/sec

    // =====================================================================
    // Phase A3 (epic #181) -- L5 Trinity runtime invariant blocks (appended)
    // phi^2 + phi^-2 = 3 is the ONLY [Verified] phi-fact. f64 tol 1e-14.
    // =====================================================================

    test test_l5_phi_sq_equals_phi_plus_one
        // [Verified] phi^2 = phi + 1 -- defining algebraic identity of phi.
        // L5 canonical. f64 check, tolerance 1e-14.
        given phi    = sacred_physics::PHI
        and   phi_sq = PHI_SQ
        when  diff   = abs(phi_sq - (phi + 1.0))
        then  diff < 1e-14

    test test_l5_phi_sq_plus_phi_inv_sq_equals_3
        // [Verified] phi^2 + phi^-2 = 3 -- the ONLY [Verified] phi-fact (L5).
        // Algebraic proof: phi^2 = phi+1, phi^-2 = 2-phi, sum = 3.
        // f64 check, tolerance 1e-14. 50-digit audit lives in pellis_verify.t27.
        given phi     = sacred_physics::PHI
        and   phi_sq  = PHI_SQ
        and   phi_inv = 1.0 / phi
        and   phi_inv2 = phi_inv * phi_inv
        when  lhs    = phi_sq + phi_inv2
        and   diff   = abs(lhs - 3.0)
        then  diff < 1e-14

    test test_l5_phi_inv2_equals_2_minus_phi
        // Verify the algebraic sub-fact: phi^-2 = 2 - phi (used in the proof above).
        // [Verified]. f64 check, tolerance 1e-14.
        given phi      = sacred_physics::PHI
        and   phi_sq   = PHI_SQ
        and   phi_inv2 = 1.0 / phi_sq
        when  diff     = abs(phi_inv2 - (2.0 - phi))
        then  diff < 1e-14

    // -----------------------------------------------------------------------
    // Invariants
    // -----------------------------------------------------------------------

    invariant l5_phi_sq_equals_phi_plus_one
        // [Verified] L5 law: phi^2 = phi + 1.
        // Tolerance 1e-14 (f64; 50-digit audit lives in pellis_verify.t27).
        // This invariant is a RUNTIME GUARD, not the high-precision audit.
        assert abs(PHI_SQ - (sacred_physics::PHI + 1.0)) < 1e-14

    invariant l5_phi_sq_plus_phi_inv_sq_equals_3
        // [Verified] L5 law: phi^2 + phi^-2 = 3.
        // The ONLY [Verified] phi-fact in this codebase (GOLDEN CHAIN Law L5).
        // All other phi claims are [Open conjecture] with a falsification path.
        // Tolerance 1e-14 (f64 runtime guard).
        // 50-digit arbitrary-precision checking belongs to the pellis audit path.
        assert abs(PHI_SQ + (1.0 / PHI_SQ) - 3.0) < 1e-14

    // -----------------------------------------------------------------------
    // Benchmark
    // -----------------------------------------------------------------------

    bench bench_l5_phi_identity_check
        // Measure cost of evaluating both L5 identities in sequence.
        // Expected: < 50ns (pure floating-point arithmetic, no allocations).
        given phi    = sacred_physics::PHI
        and   phi_sq = PHI_SQ
        when  _a = abs(phi_sq - (phi + 1.0))
        and   _b = abs(phi_sq + (1.0 / phi_sq) - 3.0)
        then elapsed_time_ns < 50
}

Open the lesson's spec in the player ↗

All lessons