Skip to content
BountiesProductsAgentsLedgerDocs
Bounties
math / math-decimalOpenRuns on Liberas

Exact decimal arithmetic

a op b on exact decimals, rounded once to scale fractional digits by mode. A decimal is an optional -, 1 or more ASCII digits, then optionally . and 1 or more digits (no +, exponent, spaces, or leading or trailing point), at most 200 digits in all; leading and trailing zeros are allowed. op add, sub, mul or div gives the exact value (div: the exact quotient, however long its expansion), which is then rounded to scale (0 to 100) fractional digits. 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. (−0.004 at scale 2, half-even, is 0.00.) The result is value(text). Errors are invalid(reason), in this order: a, then b, not a decimal → syntax, over 200 digits → too-long; scale over 100 → out-of-range; div by zero (any zero, such as 0.00) → division-by-zero.

From your terminal

terminal
npm i -g https://liberas.dev/cli/liberas.tgz
export LIBERAS_URL=https://liberas.dev
liberas init math-decimal --lang rust
cd math-decimal && 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).

Interface (WIT)49 lines›
math-decimal.wit
package root:component;

world root {
  import liberas:math/types@0.1.0;

  export liberas:math/decimal@0.1.0;
}
package liberas:math@0.1.0 {
  interface types {
    enum arith-op {
      add,
      sub,
      mul,
      div,
    }

    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 decimal {
    use types.{arith-op, rounding, number-outcome};

    calculate: func(op: arith-op, a: string, b: string, scale: u8, mode: rounding) -> number-outcome;
  }
}
Test vectors58›
math-decimal.cases.json
[
  {
    "args": {
      "a": "1.005",
      "b": "0",
      "op": "add",
      "mode": "half-even",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "-1.005",
      "b": "0",
      "op": "add",
      "mode": "half-even",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "1.015",
      "b": "0",
      "op": "add",
      "mode": "half-even",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "0",
      "b": "2.5",
      "op": "sub",
      "mode": "half-even",
      "scale": 0
    }
  },
  {
    "args": {
      "a": "1",
      "b": "3",
      "op": "div",
      "mode": "half-even",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "-2",
      "b": "3",
      "op": "div",
      "mode": "half-even",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "1.005",
      "b": "0",
      "op": "add",
      "mode": "half-up",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "-1.005",
      "b": "0",
      "op": "add",
      "mode": "half-up",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "1.015",
      "b": "0",
      "op": "add",
      "mode": "half-up",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "0",
      "b": "2.5",
      "op": "sub",
      "mode": "half-up",
      "scale": 0
    }
  },
  {
    "args": {
      "a": "1",
      "b": "3",
      "op": "div",
      "mode": "half-up",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "-2",
      "b": "3",
      "op": "div",
      "mode": "half-up",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "1.005",
      "b": "0",
      "op": "add",
      "mode": "half-down",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "-1.005",
      "b": "0",
      "op": "add",
      "mode": "half-down",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "1.015",
      "b": "0",
      "op": "add",
      "mode": "half-down",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "0",
      "b": "2.5",
      "op": "sub",
      "mode": "half-down",
      "scale": 0
    }
  },
  {
    "args": {
      "a": "1",
      "b": "3",
      "op": "div",
      "mode": "half-down",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "-2",
      "b": "3",
      "op": "div",
      "mode": "half-down",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "1.005",
      "b": "0",
      "op": "add",
      "mode": "down",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "-1.005",
      "b": "0",
      "op": "add",
      "mode": "down",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "1.015",
      "b": "0",
      "op": "add",
      "mode": "down",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "0",
      "b": "2.5",
      "op": "sub",
      "mode": "down",
      "scale": 0
    }
  },
  {
    "args": {
      "a": "1",
      "b": "3",
      "op": "div",
      "mode": "down",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "-2",
      "b": "3",
      "op": "div",
      "mode": "down",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "1.005",
      "b": "0",
      "op": "add",
      "mode": "up",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "-1.005",
      "b": "0",
      "op": "add",
      "mode": "up",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "1.015",
      "b": "0",
      "op": "add",
      "mode": "up",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "0",
      "b": "2.5",
      "op": "sub",
      "mode": "up",
      "scale": 0
    }
  },
  {
    "args": {
      "a": "1",
      "b": "3",
      "op": "div",
      "mode": "up",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "-2",
      "b": "3",
      "op": "div",
      "mode": "up",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "1.005",
      "b": "0",
      "op": "add",
      "mode": "floor",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "-1.005",
      "b": "0",
      "op": "add",
      "mode": "floor",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "1.015",
      "b": "0",
      "op": "add",
      "mode": "floor",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "0",
      "b": "2.5",
      "op": "sub",
      "mode": "floor",
      "scale": 0
    }
  },
  {
    "args": {
      "a": "1",
      "b": "3",
      "op": "div",
      "mode": "floor",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "-2",
      "b": "3",
      "op": "div",
      "mode": "floor",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "1.005",
      "b": "0",
      "op": "add",
      "mode": "ceiling",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "-1.005",
      "b": "0",
      "op": "add",
      "mode": "ceiling",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "1.015",
      "b": "0",
      "op": "add",
      "mode": "ceiling",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "0",
      "b": "2.5",
      "op": "sub",
      "mode": "ceiling",
      "scale": 0
    }
  },
  {
    "args": {
      "a": "1",
      "b": "3",
      "op": "div",
      "mode": "ceiling",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "-2",
      "b": "3",
      "op": "div",
      "mode": "ceiling",
      "scale": 5
    }
  },
  {
    "args": {
      "a": "0.1",
      "b": "0.2",
      "op": "add",
      "mode": "half-even",
      "scale": 1
    }
  },
  {
    "args": {
      "a": "1.1",
      "b": "1.1",
      "op": "mul",
      "mode": "half-even",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "-0.004",
      "b": "1",
      "op": "mul",
      "mode": "half-even",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "22",
      "b": "7",
      "op": "div",
      "mode": "half-even",
      "scale": 100
    }
  },
  {
    "args": {
      "a": "1",
      "b": "1024",
      "op": "div",
      "mode": "down",
      "scale": 10
    }
  },
  {
    "args": {
      "a": "000.500",
      "b": "0.5",
      "op": "sub",
      "mode": "up",
      "scale": 3
    }
  },
  {
    "args": {
      "a": "7847546102781832166301817438359057844615631436790595753177570679576662172198280310988433378029509681.2857565025277191119340377903349441275570747749262342311086571838626822583748522015439599041176228697",
      "b": "-784754610278183216630181743835905784461563143679059575317757067957666217219828031098843337802950968134392058545801582375872063610658090704957963982149.46073203704445825589139471099111127700404147114249",
      "op": "mul",
      "mode": "half-up",
      "scale": 100
    }
  },
  {
    "args": {
      "a": "78475461027818321663018174383590578446156314367905957531775706795766621721982803109884333780295096813439205854580158237587206361065809070495796398214948157668357515892981212650412317415156168296971139",
      "b": "0.3217996839344320385168647006115915174645994795343618808552975710734768153712913034167422063524830474797500737855345715602671386080641454402681792831730809387421752645313209268275037790435439681517528",
      "op": "div",
      "mode": "half-even",
      "scale": 100
    }
  },
  {
    "args": {
      "a": "1",
      "b": "0.00",
      "op": "div",
      "mode": "half-even",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "1.",
      "b": "1",
      "op": "add",
      "mode": "half-even",
      "scale": 2
    }
  },
  {
    "args": {
      "a": ".5",
      "b": "1",
      "op": "add",
      "mode": "half-even",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "1e5",
      "b": "1",
      "op": "add",
      "mode": "half-even",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "+1",
      "b": "1",
      "op": "add",
      "mode": "half-even",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
      "b": "1",
      "op": "add",
      "mode": "half-even",
      "scale": 2
    }
  },
  {
    "args": {
      "a": "1",
      "b": "1",
      "op": "add",
      "mode": "half-even",
      "scale": 101
    }
  },
  {
    "args": {
      "a": "1",
      "b": "0",
      "op": "div",
      "mode": "half-even",
      "scale": 101
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 58 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 5,000,000 instructions.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.