{"id":24961148,"url":"https://github.com/cag/mp-wasm","last_synced_at":"2025-04-10T21:22:38.570Z","repository":{"id":57303454,"uuid":"159215914","full_name":"cag/mp-wasm","owner":"cag","description":"Multiple precision arithmetic in JS with WASM","archived":false,"fork":false,"pushed_at":"2018-12-19T19:45:10.000Z","size":1505,"stargazers_count":4,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T18:52:30.437Z","etag":null,"topics":["arbitrary-precision","arithmetic","javascript","math","nodejs","numerical-methods","wasm","web","webassembly"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cag.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-26T18:40:35.000Z","updated_at":"2023-07-30T19:51:19.000Z","dependencies_parsed_at":"2022-09-19T13:00:14.896Z","dependency_job_id":null,"html_url":"https://github.com/cag/mp-wasm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cag%2Fmp-wasm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cag%2Fmp-wasm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cag%2Fmp-wasm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cag%2Fmp-wasm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cag","download_url":"https://codeload.github.com/cag/mp-wasm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248299029,"owners_count":21080450,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arbitrary-precision","arithmetic","javascript","math","nodejs","numerical-methods","wasm","web","webassembly"],"created_at":"2025-02-03T08:45:38.463Z","updated_at":"2025-04-10T21:22:38.527Z","avatar_url":"https://github.com/cag.png","language":"JavaScript","readme":"# mp-wasm\n\nMultiple precision arithmetic in JS with WASM.\n\nNote: This is pretty experimental, as WASM is itself somewhat experimental right now.\n\n\n## Installation\n\n### Node.JS\n\nJust grab this off of NPM:\n\n    npm i mp-wasm\n\n```js\nconst mpWasm = require('mp-wasm')\n// ...\n```\n\n### Web\n\nDownload the files `mp.wasm` and `mp-wasm.js`. In your page, just pop this in there:\n\n```html\n\u003cscript src=\"mp-wasm.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\nfetchMPWasm('mp.wasm').then((mpWasm) =\u003e {\n    // ...\n})\n\u003c/script\u003e\n```\n\n#### They're Different?\n\nYes. Synchronous feels right on Node.JS for this. Likewise, the web is built for asynchronous stuff.\n\nThe rest of this is the same though (that is, synchronous).\n\n\n## Usage\n\nThe main API object (named `mpWasm` above), currently exposes one thing: `mpf`, which is a [GNU MP floating point *reliably*](https://www.mpfr.org/) factory:\n\n```js\nconst { mpf } = mpWasm\n```\n\n`mpf` lets you make `MPFloat` instances (I will now use `mpf` to also refer to `MPFloat`):\n\n```js\nmpf(1.5e-4)\nmpf('3.563432432')\nmpf('0xdeadbeef')\nmpf('55434.3244324235454353543e10')\nmpf(99999999999999999999999999999999999999999999999n)\n```\n\nYou can also set the value of an `mpf` instance after it's initially created:\n\n```js\nmpf(54235432.432432432).set(-5)\n```\n\nIt also has a bunch of functions which accept parameters of all types:\n\n```js\nmpf.add(1, '3.56')\nmpf.sub(3247321987493214231n, mpf('7.67653431432143214324321432115e21'))\n```\n\nAll these functions are curried into methods for instances as well:\n\n```js\nmpf(55659437894732143172493127n).mul(10n**33n)\nmpf('inf').neg()\n```\n\n### Instance Configuration\n\nMany of the functions also carry similar options which may be expressed as an optional last parameter. For example,\n\n```js\nmpf(1.567e10, { prec: 3 })\n```\n\ncreates an instance of `mpf` with only three bits of precision in its significand. Likewise, a rounding mode may be specified:\n\n```js\nmpf(1.5, { roundingMode: 'roundTowardZero' })\n```\n\nOr both, if you'd like:\n\n```js\nmpf(3.5, { prec: 128, roundingMode: 'roundTowardPositive' })\n```\n\n#### Available Rounding Modes\n\nThese modes correspond with MPFR's:\n\n    | 'roundTiesToEven'     |  0  |\n    | 'roundTowardZero'     |  1  |\n    | 'roundTowardPositive' |  2  |\n    | 'roundTowardNegative' |  3  |\n    | 'roundAwayZero'       |  4  |\n    | 'roundFaithful'       |  5  |\n    | 'roundTiesToAwayZero' | -1  |\n\nEither the string or the number value may be specified as the rounding mode.\n\n### General Configuration\n\nFor getting/setting the default precision in bits used (be sure to set this to a higher precision than its default starting value `53`, which is comparable to just doing plain double float computations):\n\n    getDefaultPrec()\n    setDefaultPrec(prec)\n\nFor getting/setting the default rounding mode:\n\n    getDefaultRoundingMode() {\n    setDefaultRoundingMode(roundingMode) {\n\nPast `mpf` instances do not get updated when this parameter changes. Future new instances and computations will use these default settings though.\n\n### Constants\n\nThese functions gets a corresponding math constant and returns an shared instance of it:\n\n    getLog2(opts)\n    getPi(opts)\n    getEuler(opts)\n    getCatalan(opts)\n\n### Operators\n\nUnary operators:\n\n    sqr       sqrt      recSqrt    cbrt    neg     abs\n    log       log2      log10      log1p\n    exp       exp2      exp10      expm1\n    cos       sin       tan        sec     csc     cot\n    acos      asin      atan     \n    cosh      sinh      tanh       sech    csch    coth\n    acosh     asinh     atanh      fac\n    eint      li2       gamma      lngamma      digamma\n    zeta      erf       erfc       j0    j1    y0   y1\n    rint      rintCeil  rintFloor  rintRound\n    rintRoundeven       rintTrunc  frac\n\nBinary operators:\n\n    add       sub       mul        div\n    rootn     pow       dim\n    atan2        gammaInc     beta\n    jn        yn        agm       hypot\n    fmod      remainder\n    min       max\n\nAnd comparison operators `\u003c` (`lt`), `\u003c=` (`lte`), `\u003e` (`gt`) `\u003e=` (`gte`), `==` (`eq`), and `\u003c\u003e` (`lgt`) exist, with the latter two distinguishing between edge cases with NaN values. All of the comparison operators are on the prototype and must be called as a method on the instance:\n\n    mpf('1.0').lt('1.0000000000000000000000000000001')\n\nCast these `mpf`s to JS primitives with:\n\n    toString()\n    toNumber(opts)\n\nThe following checks are also available:\n\n    isNaN()\n    isFinite()\n    isInteger()\n\n### Internal Representation\n\n`mpf` instances can have their internal representation info acquired with the following methods:\n\n* `isSignBitSet()`\n\n  This returns `true` if number is a negative number/zero/infinity, and `false` otherwise.\n\n* `getBinaryExponent()`\n\n  This returns the `exponent` for a given number `x` when `x` is written in the form `significand * 2^exponent`, where `significand` is in the range `[0.5, 1)`. Also, for some special cases:\n\n  * If `x` is ±0, return −∞\n  * If `x` is ±∞, return +∞\n  * If `x` is NaN, return NaN\n\n* `getSignificandRawBytes()`\n\n  This returns a Uint8Array of the bytes of the significand in little-endian order padded to a multiple of the WASM machine word size. The least significant bits of this array are zeroes according to the precision of the `mpf`. Unlike IEEE 754, there is no leading bit convention, and the significand will typically have its most significant bit explicitly set.\n\n## Future Stuff\n\n### Add MPC, MPFI, and/or iRRAM?\n\nAt least some complex ops would be nice.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcag%2Fmp-wasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcag%2Fmp-wasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcag%2Fmp-wasm/lists"}