Skip to content

feat(lang): add %, floor, ceil, round, min, max, clamp, pow and a seeded rand - #211

Open
tupizz wants to merge 5 commits into
wrong-nebula:mainfrom
tupizz:feat/lang-expression-maths
Open

tupizz wants to merge 5 commits into
wrong-nebula:mainfrom
tupizz:feat/lang-expression-maths

Conversation

@tupizz

@tupizz tupizz commented Dec 5, 2026

Copy link
Copy Markdown

Summary

Forma-Pipe expressions get a % operator and eight functions: floor, ceil, round, min, max, clamp, pow and a seeded rand(i, seed).
With them a grid, a seeded scatter or a clamped size fits in one at=[...] comprehension.
This is item 4 of the field report in #167, where the author ran Blender only to generate 21 apple positions and pasted them into the Forma script.

main This PR
at=[(i % 3, floor(i / 3), 0) for i in range(12)] unexpected character '%' a 3 x 4 grid
at=[(rand(i, 1), rand(i, 2), 0) for i in range(21)] unknown function 'rand' the same 21 positions on every run and on native and WASM
max(0.9, min(1.7, pow(1.17, i))) unknown function 'max' a clamped growth curve
MCP tools/list 1,006 tokens 1,006 tokens
Native forma 2,835,328 B 2,835,712 B (+384)
wasm/forma.wasm 1,126,481 B 1,132,579 B (+6,098)

Before

Four scenes written the way an agent would write them, evaluated on main at 16fb483:

$ forma eval --strict --scene --out crate.glb scenes/apple_crate.forma
ERR: line 7: unexpected character '%'
$ forma eval --strict --scene --out carrier.glb scenes/carrier.forma
ERR: line 6: floor(): unknown function 'floor'; accepted: box, cyl, ...

The gym and the arena stop at % in the same way.

After

The same four scripts, unchanged, on this branch:

Apple crates (#167) Gym
Two crates with 21 apples scattered by rand, top view 24 floor tiles and six dumbbells growing along a rack
jitter(i, seed) = (rand(i, seed) - 0.5) * 1.4, rows by i % 4, tiers by floor(i / 12) tiles by i % 6 and ceil, dumbbell size max(0.9, min(1.7, pow(1.17, i - 1)))
Arena Bottle carrier
12 runners in staggered rows, lane marks and a seeded crowd on two terraces Six bottles of clamped, rounded heights in a carrier
alternate rows offset by 200 * (row(i) % 2), crowd jittered by rand(i, seed=8) heights round(clamp(13.2 + 2.75 * i, 16, 25))

How it behaves

  • a % b is the floored remainder, as in Python: the result takes the sign of b, so -1 % 3 is 2 and a negative index still lands in the grid. It binds like * and /. % 0 is an error.
  • round takes halves away from zero. min and max take two or more numbers. clamp(x, lo, hi) rejects lo > hi.
  • Every new function errors on a non-finite result, as sqrt does, and a zero result is 0, never -0.
  • rand(i, seed=0) is a pure function with no generator state: SplitMix64 over the IEEE-754 bits of both arguments, (mix(bits(i) ^ mix(bits(seed))) >> 11) / 2^53. The same whole-number arguments give the same number on every run, platform and call order.
  • rand takes whole numbers from -2^32 to 2^32 only. pow, sin, cos and atan2 come from the platform maths library and can differ in the last bit between native and WASM (sin, cos and atan2 already do on main). Since rand hashes bits, rand(pow(2, 1.5)) would put a part in a different place on the two builds; with this rule it is an error on both instead. The limit is 2^32 rather than 2^53 because from 2^52 up every double is whole, so a last-bit difference would pass as two different seeds.
  • The new names stay usable as part and variable names, as sqrt already is, because a call is recognised by the (. floor = box(...), which this repository's own tests use, keeps working. Only def floor(...) is now rejected, as def sqrt(...) is.
  • Not added on purpose: comparisons and conditionals (they belong to feat(lang): add assert statements to Forma-Pipe #133's assert), indexing, vector arithmetic, and any change to the limits.

Evidence

  • 29 new tests in crates/forma-lang/tests/expression_maths.rs, written first; 27 fail on main and 2 guard behaviour that must not change. tests/npm_smoke.mjs checks that rand(pow(2, 1.5)) and rand(pow(3, 33.2)) fail with the same message on the npm CLI.
  • The five commands in .github/workflows/ci.yml pass on the head with Rust 1.100.1 on macOS arm64: clippy with -D warnings clean, 215 tests pass (186 on main), npm run build:wasm and npm test 5/5, path guard OK. Each of the five commits builds and passes clippy and the forma-lang tests on its own.
  • Sizes and tokens were measured on main and on the branch with the same toolchain and binaryen 134, and measured again independently with the same result. The MCP tool list is byte-identical because the names are not added to the always-loaded spec line; listing them there would cost 9 tokens, which I left for you to decide. Of the 6,098 WASM bytes, 2,428 are the exact % (fmod). A multiplication-based remainder would save 2,399 B but gives wrong answers for decimals (7.3 % 0.1 comes out as -8.9e-16), so the exact one stays.
  • An independent acceptance pack, written from the specification without seeing the code, ran 261 checks on the head, all passing, again after rebasing on 16fb483: the four scenes above (161 named parts) compared with positions computed in Python and with the same layouts built in Blender 5.2.1, every exported GLB passing the Khronos validator with zero errors and zero warnings, repeat runs byte-identical, native and npm CLI agreeing on every position, and 105 hostile inputs (zero divisors, overflowing powers, signed zero, 512 nested calls, the new names as parts and variables) giving the defined value or error and never a crash or a hang.
  • 21,600 generated expressions over the nine names matched Python bit for bit on native. Native and WASM agree bit for bit on floor, ceil, round, min, max, clamp, % and rand; pow differs in the last bit in 194 of 2,400 inputs, as sin (49 of 1,500), cos (57) and atan2 (66) already do on main.
  • The branch merges cleanly with fix(lang): reject a second expression on the same line #182, and with feat(lang): add assert statements to Forma-Pipe #133 after resolving the same one-line spec conflict feat(lang): add assert statements to Forma-Pipe #133 already has with main; the combined suites pass.

Merge danger

  • Low for existing scripts. No existing name changes meaning, and part and variable names like floor, min or max keep working. Bare min and max are still selector keywords, as in align(z, min). The one script that would break is one that defines a macro called floor, ceil, round, min, max, clamp, pow or rand; there is none in this repository.
  • WASM grows by 6,098 B on a budget that is already over (Size and token budgets: all three are over on main, and CI does not check them #191). Native grows by 384 B and the MCP token count does not move.
  • floor, ceil, round and % of a sin, cos, atan2 or pow value can differ between native and WASM when it sits exactly on a boundary, as in any language that uses the platform maths library. The language reference says so. Removing it for good means using one maths implementation in both builds, which is a separate question.

Not verified: GitHub's Linux CI and Linux x86_64 builds; maths libraries other than macOS and the Rust WASM port; the long fuzz test CI skips.

Refs #167

`a % b` is the floored remainder, as in Python: the result takes the
sign of `b`, so `-1 % 3` is 2 and an index that runs negative still
lands inside a grid. It binds like `*` and `/` and associates left to
right. `a % 0` fails as an arithmetic error that names the operator.
Agents reach for these names because Python has them, and today each
one fails as an unknown function.

- floor, ceil and round take one number; round takes halves away from
  zero.
- min and max take two or more numbers. Bare `min` and `max` stay
  selector keywords, as in `align(z, min)`.
- clamp(x, lo, hi) rejects lo greater than hi.
- pow(base, exp) uses the platform power function, like sin and cos.

Every result must be finite, as with sqrt, so a literal that reads as
infinity (such as 1e309) cannot pass through these functions. A zero
result is always +0, so `round(-0.4)` never prints or exports as -0.
The names are reserved the way sqrt is: a macro cannot take them, a
variable or part still can.
rand(i, seed) returns a number in [0, 1) fixed by its two arguments;
seed defaults to 0 and may be passed as `seed=`. It is a pure hash with
no generator state, so the same arguments give the same number on every
run, in native and in WASM, whatever order the calls run in. A scatter
from the loop index and a literal seed, such as
`at=[(4 * rand(i, 1), 4 * rand(i, 2), 0) for i in range(21)]`, gives the
same scene everywhere.

Both arguments must be whole numbers from -2^32 to 2^32. The hash reads
every bit, and sin, cos, atan2 and pow can differ in the last bit
between native and WASM. A fraction such as `pow(2, 1.5)` is rejected;
from 2^52 up every double is whole, so `pow(3, 33.2)` would reach the
hash as two different whole numbers, and the bound rejects it too.
Below 2^32 a last-bit difference cannot turn one whole number into
another, so such a value passed straight in fails on at least one
target instead of moving a part; a whole number computed from it, for
example with round or a subtraction, can still differ. The error
leaves the value out, so it reads the same on every target;
tests/npm_smoke.mjs checks both cases on the WASM CLI.

The value is SplitMix64 applied to the IEEE-754 bits of both arguments
(-0 taken as 0): (mix(bits(i) ^ mix(bits(seed))) >> 11) / 2^53. The tests
pin fourteen values computed independently from that definition.
`%` joins `*` and `/` in mul-expr, so grammar-constrained decoding can
emit it. The new functions need no grammar change: they are ordinary
calls through the generic `call` rule.
The language reference gets an "Expression maths" table with each
name's meaning and errors, plus a grid and a scatter example. SKILL.md,
llms.txt and AGENTS.md list the names where they already list atan2.

The always-loaded spec line in forma-lang (SPEC_PIPE) is left as it is,
so the MCP tool list keeps its token count.
@tupizz
tupizz requested a review from wrong-nebula as a code owner December 5, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant