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

Exact fractions

a op b (add, sub, mul, div) on exact fractions. A fraction is an integer (an optional - then 1 or more ASCII digits) or n/d: an optional -, digits, /, digits (no sign on d, no spaces, one /); n and d at most 1,000 digits each, leading zeros allowed. The result is value(text), reduced to lowest terms with a positive denominator: n alone when the denominator is 1 (so 0, never 0/1), else n/d with the sign on n. Errors are invalid(reason), in this order: a, then b, each: not in the syntax → syntax, a part over 1,000 digits → too-long, d = 0 → division-by-zero; then div by a zero value → 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-rational --lang rust
cd math-rational && 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)39 lines›
math-rational.wit
package root:component;

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

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

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

    calculate: func(op: arith-op, a: string, b: string) -> number-outcome;
  }
}
Test vectors16›
math-rational.cases.json
[
  {
    "args": {
      "a": "1/2",
      "b": "1/3",
      "op": "add"
    }
  },
  {
    "args": {
      "a": "1/2",
      "b": "1/2",
      "op": "sub"
    }
  },
  {
    "args": {
      "a": "-2/4",
      "b": "6/3",
      "op": "mul"
    }
  },
  {
    "args": {
      "a": "3/4",
      "b": "-9/8",
      "op": "div"
    }
  },
  {
    "args": {
      "a": "-0/5",
      "b": "007/014",
      "op": "add"
    }
  },
  {
    "args": {
      "a": "10/3",
      "b": "3/10",
      "op": "mul"
    }
  },
  {
    "args": {
      "a": "7847546102781832166301817438359057844615631436790595753177570679576662172198280310988433378029509681343920585458015823758720636106580907049579639821494815766835751589298121265041231741515616829697113968578091135398658076978023493787372398325828638735406060223590930309468174570847346196949028591810977347152031297630622480144960819311362782099746248621805146153877658545183532427216392254887308994057193285078348590291716707472080133024951898528829304777108406545498113419306861006599233607534219168339074486909297176709338107674750430920260714888551531324169494701371584969672692005808516519229151588751297526080642681449964003259013150298062709004347563771971001254217510128643868274956481107392970130985144073077852731259120769804870914317702660374586154343289942805677942553632934693859776274961572737511548746022576872336853294327993312122687066612449440029351704665304019252107848120631866731955643301866905905268347942678815687243479915238003810836627098406370227095442761582003029411566957139/321799683934432038516864700611591517464599479534361880855297571073476815371291303416742206352483047479750073785534571560267138608064145440268179283173080938742175264531320926827503779043543968151752808794546918612507568345130076864361287500151605649563207207757369154497684664340226836701247795988631398280117979749202357254926460230287863126449576532876046516142630208670648861328906731166620406638385183729591241186373336202732908059535274381252692453420069931974594062100465304652238863188223595319045073750722843262701032494166520109814978410257912111248261029404231646077952403445318408317504006253314717616671023876728906846532085321798976352802418324936315027523293248845702725676704324703326117045039195035979939985643839475243301937785864815795519980794661251084518993767038963073449803733104138572440370538162215806760623863409033038765108957001950219215031556581643190046814692356936475824576881244834545684516831174433953696693935802261355670549420562995269510818977992922148304399615428",
      "b": "-5097751884511711724731247202787686349115961764508950419861776548650510666450735072529941034623088094555710646158815954340034321993214069229915569396762397528148753217957315210178184789875076205757832129795245629795365343363513442015842285318882342265059081135588467423552890998128102748199266775383645611148455347061326361895912299820980348110954605328931407697183340419498890085289343986220401840505177046185690154017773538216803293973650907350524619659309069497738540005300247021529699184393277577761262478708291459018379712929099334849803789690565846783429052834093430018574723058167880486837989420647507831423861978708228039397800858259624736611039828825637799263271771403065999260895217327369907650351756663868974565054499560693158168847490109767754675225711492841296517745151625163940982154636428596631827724805989360388248261892843871703184029441294621749759546715240275411134392525415410741985809943102625399005704665891777443708854680110378883904049188016661946483914785704117206247791559090/2857565025277191119340377903349441275570747749262342311086571838626822583748522015439599041176228697868910916000270949418463015910373615597334877235739295218494708349341816752970827769410617324947081819550556513370090430682210751442237180084043093540427086868082543956683054352496941158857569596204032436014386657649073896056755397298182414956569640070941749233764018133792412442707056345334904854222093216660758443251710246966284910158308961248075465825809447155760670484006391052077311223218719691348589718908773727468039165755431803851464705298096492348717449647058124628875976024411406100469949138111467465334858710748716041639280002749415740190085412656341319768716516722688979784838941857003743580601165480286955276205731276918722826277312561387509256198575557031151405518288316530916561127302154211987491799224367282028312733235850211126503017978955896357309238950134514511743253672926126790740841138761350613260343054854115914542625302898432004909655980497419358382535786585693520971572405816",
      "op": "add"
    }
  },
  {
    "args": {
      "a": "7847546102781832166301817438359057844615631436790595753177570679576662172198280310988433378029509681343920585458015823758720636106580907049579639821494815766835751589298121265041231741515616829697113968578091135398658076978023493787372398325828638735406060223590930309468174570847346196949028591810977347152031297630622480144960819311362782099746248621805146153877658545183532427216392254887308994057193285078348590291716707472080133024951898528829304777108406545498113419306861006599233607534219168339074486909297176709338107674750430920260714888551531324169494701371584969672692005808516519229151588751297526080642681449964003259013150298062709004347563771971001254217510128643868274956481107392970130985144073077852731259120769804870914317702660374586154343289942805677942553632934693859776274961572737511548746022576872336853294327993312122687066612449440029351704665304019252107848120631866731955643301866905905268347942678815687243479915238003810836627098406370227095442761582003029411566957139/3217996839344320385168647006115915174645994795343618808552975710734768153712913034167422063524830474797500737855345715602671386080641454402681792831730809387421752645313209268275037790435439681517528087945469186125075683451300768643612875001516056495632072077573691544976846643402268367012477959886313982801179797492023572549264602302878631264495765328760465161426302086706488613289067311666204066383851837295912411863733362027329080595352743812526924534200699319745940621004653046522388631882235953190450737507228432627010324941665201098149784102579121112482610294042316460779524034453184083175040062533147176166710238767289068465320853217989763528024183249363150275232932488457027256767043247033261170450391950359799399856438394752433019377858648157955199807946612510845189937670389630734498037331041385724403705381622158067606238634090330387651089570019502192150315565816431900468146923569364758245768812448345456845168311744339536966939358022613556705494205629952695108189779929221483043996154284",
      "b": "5097751884511711724731247202787686349115961764508950419861776548650510666450735072529941034623088094555710646158815954340034321993214069229915569396762397528148753217957315210178184789875076205757832129795245629795365343363513442015842285318882342265059081135588467423552890998128102748199266775383645611148455347061326361895912299820980348110954605328931407697183340419498890085289343986220401840505177046185690154017773538216803293973650907350524619659309069497738540005300247021529699184393277577761262478708291459018379712929099334849803789690565846783429052834093430018574723058167880486837989420647507831423861978708228039397800858259624736611039828825637799263271771403065999260895217327369907650351756663868974565054499560693158168847490109767754675225711492841296517745151625163940982154636428596631827724805989360388248261892843871703184029441294621749759546715240275411134392525415410741985809943102625399005704665891777443708854680110378883904049188016661946483914785704117206247791559090/2857565025277191119340377903349441275570747749262342311086571838626822583748522015439599041176228697868910916000270949418463015910373615597334877235739295218494708349341816752970827769410617324947081819550556513370090430682210751442237180084043093540427086868082543956683054352496941158857569596204032436014386657649073896056755397298182414956569640070941749233764018133792412442707056345334904854222093216660758443251710246966284910158308961248075465825809447155760670484006391052077311223218719691348589718908773727468039165755431803851464705298096492348717449647058124628875976024411406100469949138111467465334858710748716041639280002749415740190085412656341319768716516722688979784838941857003743580601165480286955276205731276918722826277312561387509256198575557031151405518288316530916561127302154211987491799224367282028312733235850211126503017978955896357309238950134514511743253672926126790740841138761350613260343054854115914542625302898432004909655980497419358382535786585693520971572405816",
      "op": "mul"
    }
  },
  {
    "args": {
      "a": "5",
      "b": "1/0",
      "op": "div"
    }
  },
  {
    "args": {
      "a": "5",
      "b": "0/7",
      "op": "div"
    }
  },
  {
    "args": {
      "a": "1/-2",
      "b": "1",
      "op": "add"
    }
  },
  {
    "args": {
      "a": "1/2/3",
      "b": "1",
      "op": "add"
    }
  },
  {
    "args": {
      "a": "1 /2",
      "b": "1",
      "op": "add"
    }
  },
  {
    "args": {
      "a": "/2",
      "b": "1",
      "op": "add"
    }
  },
  {
    "args": {
      "a": "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111/3",
      "b": "1",
      "op": "add"
    }
  },
  {
    "args": {
      "a": "1/11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
      "b": "1",
      "op": "add"
    }
  }
]
Verification6›
  • Outputs must match the private reference implementation bit for bit.
  • Cases are the spec's 16 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 150,000,000 instructions.
  • Unbuilt, the bounty rises 10% of its shares every 7 days, up to +100%.