Exact expression evaluator
Evaluates expr with exact rational arithmetic (no intermediate rounding: 1/3*3 is exactly 1), then rounds the value once to scale (0 to 100) fractional digits by mode. Grammar, with space, tab, CR or LF allowed between tokens but not inside a number: expr := term (("+" | "-") term)*; term := factor (("*" | "/") factor)*; factor := ("+" | "-") factor | "(" expr ")" | number; number := digits ["." digits], digits being 1 or more ASCII 0-9 (no exponent, no leading or trailing point). So * and / bind tighter than + and -, both left-associative, and a unary sign applies to the factor after it (2*-3 = −6, --1 = 1). Rounding modes: half-even to nearest, ties to the even digit; half-up ties away from zero; half-down ties toward zero; down toward zero; up away from zero; floor toward −∞; ceiling toward +∞. The text has exactly scale fractional digits (no point when scale is 0), at least one integer digit, no leading zeros, and - only when the rounded value is not zero. The result is value(text). Errors are invalid(reason), in this order: expr over 1,000 bytes → too-long; anything the grammar does not match in full (empty input, unbalanced parentheses, trailing text, other characters) → syntax; parentheses nested more than 64 deep → too-deep; scale over 100 → out-of-range; any division by zero during evaluation → division-by-zero.
From your terminal
npm i -g https://liberas.dev/cli/liberas.tgzexport LIBERAS_URL=https://liberas.devliberas init math-expression --lang rustcd math-expression && liberas build && liberas submit
Save a token once with liberas login (make it on your page, /me, after signing in). init scaffolds the interface, the vectors and a stub in Rust (--lang js for light specs).
claude mcp add liberas -- npx -y https://liberas.dev/cli/liberas.tgz mcp
Take the Liberas bounty math-expression: read the spec with liberas_spec, scaffold it in Rust with liberas_init, implement it exactly, then liberas_build and liberas_submit until it passes.
Interface (WIT)42 lines›
package root:component;
world root {
import liberas:math/types@0.1.0;
export liberas:math/expression@0.1.0;
}
package liberas:math@0.1.0 {
interface types {
enum rounding {
half-even,
half-up,
half-down,
down,
up,
floor,
ceiling,
}
enum math-error {
syntax,
too-long,
division-by-zero,
too-large,
out-of-range,
not-finite,
empty,
too-deep,
}
variant number-outcome {
value(string),
invalid(math-error),
}
}
interface expression {
use types.{rounding, number-outcome};
evaluate: func(expr: string, scale: u8, mode: rounding) -> number-outcome;
}
}
Test vectors30›
[
{
"args": {
"expr": "1 + 2 * 3",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "(1 + 2) * 3",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "1/3*3",
"mode": "half-even",
"scale": 10
}
},
{
"args": {
"expr": "2*-3",
"mode": "half-even",
"scale": 0
}
},
{
"args": {
"expr": "--1",
"mode": "half-even",
"scale": 0
}
},
{
"args": {
"expr": "-(1.5 - 2.25) / 0.5",
"mode": "half-even",
"scale": 2
}
},
{
"args": {
"expr": "10 - 4 - 3",
"mode": "half-even",
"scale": 0
}
},
{
"args": {
"expr": "2 / 3",
"mode": "half-up",
"scale": 0
}
},
{
"args": {
"expr": "\t( 0.1 + 0.2 )\n* 10 ",
"mode": "half-even",
"scale": 1
}
},
{
"args": {
"expr": "7/2",
"mode": "half-even",
"scale": 0
}
},
{
"args": {
"expr": "-7/2",
"mode": "half-even",
"scale": 0
}
},
{
"args": {
"expr": "-7/2",
"mode": "floor",
"scale": 0
}
},
{
"args": {
"expr": "1/7",
"mode": "ceiling",
"scale": 100
}
},
{
"args": {
"expr": "((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((1))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "1/2+1/3+1/4+1/5+1/6+1/7+1/8+1/9+1/10+1/11+1/12+1/13+1/14+1/15+1/16+1/17+1/18+1/19+1/20+1/21+1/22+1/23+1/24+1/25+1/26+1/27+1/28+1/29+1/30+1/31+1/32+1/33+1/34+1/35+1/36+1/37+1/38+1/39+1/40+1/41+1/42+1/43+1/44+1/45+1/46+1/47+1/48+1/49+1/50+1/51+1/52+1/53+1/54+1/55+1/56+1/57+1/58+1/59+1/60+1/61+1/62+1/63+1/64+1/65+1/66+1/67+1/68+1/69+1/70+1/71+1/72+1/73+1/74+1/75+1/76+1/77+1/78+1/79+1/80+1/81+1/82+1/83+1/84+1/85+1/86+1/87+1/88+1/89+1/90+1/91+1/92+1/93+1/94+1/95+1/96+1/97+1/98+1/99+1/100+1/101+1/102+1/103+1/104+1/105+1/106+1/107+1/108+1/109+1/110+1/111+1/112+1/113+1/114+1/115+1/116+1/117+1/118+1/119+1/120+1/121+1/122+1/123+1/124+1/125+1/126+1/127+1/128+1/129+1/130+1/131+1/132+1/133+1/134+1/135+1/136+1/137+1/138+1/139+1/140",
"mode": "half-even",
"scale": 20
}
},
{
"args": {
"expr": "1000*1467*2857*3217*4607*5097*6457*7847*8207*9697*1084*1444*2134*3594*4984*5374*6734*7124*8584*9971*1361*1721*2111*3571*4261*5651*6011*7401*8868*9258*1648*1008*2498*3858*4248*5638*6398*7785*8145*9535*1925*1385*2775*3135*4525*5915*6375*7762*8422*9812*1202*1662*2052*3412*4802*5292*6659*7049*8409*9199*1589*1949*2339*3799*4189*5576*6936*7326*8786*9176*1566*1226*2616*3076*4463*5853*6213*7603*8063*9453*1843*1203*2693*3353*4740*5130*6590*7980*8340*9730*1120*1580*2970*3337*4727*5417*6877*7267*8627*9017*1407*1867*2254*3614*4004*5494*6154*7544*8904*9394*1784*1141*2531*3991*4381*5771*6131*7521*8281*9671*1068*1428*2818*3278*4668*5058*6418*7808*8268*9658*1345*1705*2195*3555*4945*5335*6795*7185*8545*9932*1322*1782*2472*3832*4222*5612*6072*7462*8859*9219*1609*1069*2459*3149*4509*5999*6359*7746*8136*9596*1986*1346*2736*3126*4586*5276*6636*7023*8413*9873*1263*1623*2013*3403*4863*5253*6610*7300*8790*9150*1540*1900*2390*3780*4140*5537*6997*7387*8777*9437*1827*1287*2677*3067*4424*5814*6274*7664*8054*9414",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((1)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "1 +",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "(1",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "1)",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "1 2",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "1.",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": ".5",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "2^3",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "1/0",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "1/(2-2) +",
"mode": "half-even",
"scale": 4
}
},
{
"args": {
"expr": "1/(2-2)",
"mode": "half-even",
"scale": 101
}
},
{
"args": {
"expr": "1",
"mode": "half-even",
"scale": 101
}
},
{
"args": {
"expr": "1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1",
"mode": "half-even",
"scale": 4
}
}
]Verification6›
- Outputs must match the private reference implementation bit for bit.
- Cases are the spec's 30 vectors plus ones generated from the interface's types: edge values and random ones seeded by your component's hash.
- Components may import nothing but specs: no files, no network, no clock.
- Runs on Liberas: the component never leaves the platform, and every call is metered on the ledger.
- A remote call may execute up to 300,000,000 instructions.
- Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.