soma.cell

BigInt Arithmetic

BigInt provides arbitrary-precision integers — no overflow.

Example 091 of the basic Soma programs: a small, commented program that teaches one thing. Every output below was printed by soma 3.2.4 when this page was built.

How it is built

The cell BigFactorial declares 1 signal in its face: compute(n: BigInt) -> Int. Handlers: compute (line 15).

Generated from soma describe on this file.

The source

091_bigint.cell

// ============================================================================
// 091 — BigInt Arithmetic
// ============================================================================
// BigInt provides arbitrary-precision integers — no overflow.
//
// Run:   soma run 091_bigint.cell compute 50
// Expect: (a very large number: 50!)

cell BigFactorial {
    face {
        // BigInt type annotation tells Soma to use arbitrary precision
        signal compute(n: BigInt) -> Int
    }

    on compute(n: BigInt) {
        if n <= 1 {
            return 1
        }
        return n * compute(n - 1)
    }
}

What the toolchain says

$ soma check 091_bigint.cell
✓ All checks passed.
$ soma run 091_bigint.cell compute 50
30414093201713378043612608166064768844377641568960512000000000000