soma.cell

Arithmetic Operations

Demonstrates +, -, *, /, % on integers and floats.

Example 004 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 Arithmetic has no face and no memory. Handlers: run (line 24).

Generated from soma describe on this file.

The source

004_arithmetic.cell

// ============================================================================
// 004 — Arithmetic Operations
// ============================================================================
// Demonstrates +, -, *, /, % on integers and floats.
//
// Run:   soma run 004_arithmetic.cell run
// Expect:
//   === Integer Arithmetic ===
//   10 + 3 = 13
//   10 - 3 = 7
//   10 * 3 = 30
//   10 / 3 = 3
//   10 % 3 = 1
//   === Float Arithmetic ===
//   3.14 * 2.0 = 6.28
//   10.0 / 3.0 = 3.3333333333333335
//   === Useful Functions ===
//   abs(-42) = 42
//   to_float(7) = 7.0
//   to_int(3.14) = 3 (note: no rounding shown, just conversion test)

cell Arithmetic {

    on run() {
        print("=== Integer Arithmetic ===")
        let a = 10
        let b = 3

        // Standard math operators
        let sum = a + b
        let diff = a - b
        let prod = a * b
        let quot = a / b       // Integer division — truncates
        let rem = a % b        // Modulo — remainder

        print("{a} + {b} = {sum}")
        print("{a} - {b} = {diff}")
        print("{a} * {b} = {prod}")
        print("{a} / {b} = {quot}")
        print("{a} % {b} = {rem}")

        print("=== Float Arithmetic ===")
        let pi = 3.14
        let two = to_float(2)
        let circle = pi * two
        print("{pi} * {two} = {circle}")

        let ten = to_float(10)
        let three = to_float(3)
        let precise = ten / three
        print("{ten} / {three} = {precise}")

        print("=== Useful Functions ===")
        // abs() returns absolute value
        let neg = -42
        let pos = abs(neg)
        print("abs({neg}) = {pos}")

        // to_float converts int to float
        let converted = to_float(7)
        print("to_float(7) = {converted}")

        return "done"
    }
}

What the toolchain says

$ soma check 004_arithmetic.cell
✓ All checks passed.
$ soma run 004_arithmetic.cell run
=== Integer Arithmetic ===
10 + 3 = 13
10 - 3 = 7
10 * 3 = 30
10 / 3 = 3.3333333333333335
10 % 3 = 1
=== Float Arithmetic ===
3.14 * 2.0 = 6.28
10.0 / 3.0 = 3.3333333333333335
=== Useful Functions ===
abs(-42) = 42
to_float(7) = 7.0
done