Repository navigation
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Forma-Pipe expressions get a
%operator and eight functions:floor,ceil,round,min,max,clamp,powand a seededrand(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.
mainat=[(i % 3, floor(i / 3), 0) for i in range(12)]unexpected character '%'at=[(rand(i, 1), rand(i, 2), 0) for i in range(21)]unknown function 'rand'max(0.9, min(1.7, pow(1.17, i)))unknown function 'max'tools/listformawasm/forma.wasmBefore
Four scenes written the way an agent would write them, evaluated on
mainat16fb483:The gym and the arena stop at
%in the same way.After
The same four scripts, unchanged, on this branch:
jitter(i, seed) = (rand(i, seed) - 0.5) * 1.4, rows byi % 4, tiers byfloor(i / 12)i % 6andceil, dumbbell sizemax(0.9, min(1.7, pow(1.17, i - 1)))200 * (row(i) % 2), crowd jittered byrand(i, seed=8)round(clamp(13.2 + 2.75 * i, 16, 25))How it behaves
a % bis the floored remainder, as in Python: the result takes the sign ofb, so-1 % 3is2and a negative index still lands in the grid. It binds like*and/.% 0is an error.roundtakes halves away from zero.minandmaxtake two or more numbers.clamp(x, lo, hi)rejectslo > hi.sqrtdoes, and a zero result is0, 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.randtakes whole numbers from-2^32to2^32only.pow,sin,cosandatan2come from the platform maths library and can differ in the last bit between native and WASM (sin,cosandatan2already do onmain). Sincerandhashes 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.sqrtalready is, because a call is recognised by the(.floor = box(...), which this repository's own tests use, keeps working. Onlydef floor(...)is now rejected, asdef sqrt(...)is.assert), indexing, vector arithmetic, and any change to the limits.Evidence
crates/forma-lang/tests/expression_maths.rs, written first; 27 fail onmainand 2 guard behaviour that must not change.tests/npm_smoke.mjschecks thatrand(pow(2, 1.5))andrand(pow(3, 33.2))fail with the same message on the npm CLI..github/workflows/ci.ymlpass on the head with Rust 1.100.1 on macOS arm64: clippy with-D warningsclean, 215 tests pass (186 onmain),npm run build:wasmandnpm test5/5, path guard OK. Each of the five commits builds and passes clippy and theforma-langtests on its own.mainand 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.1comes out as-8.9e-16), so the exact one stays.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.floor,ceil,round,min,max,clamp,%andrand;powdiffers in the last bit in 194 of 2,400 inputs, assin(49 of 1,500),cos(57) andatan2(66) already do onmain.main; the combined suites pass.Merge danger
floor,minormaxkeep working. Bareminandmaxare still selector keywords, as inalign(z, min). The one script that would break is one that defines a macro calledfloor,ceil,round,min,max,clamp,poworrand; there is none in this repository.main, and CI does not check them #191). Native grows by 384 B and the MCP token count does not move.floor,ceil,roundand%of asin,cos,atan2orpowvalue 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