{"id":18355422,"url":"https://github.com/charto/bigfloat","last_synced_at":"2025-07-19T10:34:49.700Z","repository":{"id":48160950,"uuid":"47335863","full_name":"charto/bigfloat","owner":"charto","description":"Fast arbitrary precision math library for computational geometry.","archived":false,"fork":false,"pushed_at":"2018-11-22T08:16:19.000Z","size":537,"stargazers_count":77,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-21T23:51:15.164Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/charto.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":"2015-12-03T14:06:33.000Z","updated_at":"2024-10-31T23:42:37.000Z","dependencies_parsed_at":"2022-09-14T19:40:24.254Z","dependency_job_id":null,"html_url":"https://github.com/charto/bigfloat","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charto%2Fbigfloat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charto%2Fbigfloat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charto%2Fbigfloat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/charto%2Fbigfloat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/charto","download_url":"https://codeload.github.com/charto/bigfloat/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247484352,"owners_count":20946384,"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":[],"created_at":"2024-11-05T22:06:52.402Z","updated_at":"2025-04-06T12:32:01.458Z","avatar_url":"https://github.com/charto.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bigfloat\n\n[![build status](https://travis-ci.org/charto/bigfloat.svg?branch=master)](http://travis-ci.org/charto/bigfloat)\n[![npm version](https://img.shields.io/npm/v/bigfloat.svg)](https://www.npmjs.com/package/bigfloat)\n[![dependency status](https://david-dm.org/charto/bigfloat.svg)](https://david-dm.org/charto/bigfloat)\n[![install size](https://packagephobia.now.sh/badge?p=bigfloat)](https://packagephobia.now.sh/result?p=bigfloat)\n[![license](https://img.shields.io/npm/l/bigfloat.svg)](https://raw.githubusercontent.com/charto/bigfloat/master/LICENSE)\n\n`bigfloat` is a fast arbitrary precision math library optimized for computational geometry and geoinformatics.\nIt provides binary floating point:\n\n- conversion to / from the JavaScript number type, `x = new BigFloat32(123.456)` and `x.valueOf()`\n- addition, `x.add(y)`\n- subtraction, `x.sub(y)`\n- multiplication, `x.mul(y)`\n- comparison, `x.deltaFrom(y)` alias `x.cmp(y)`\n- string output in even bases 2-36, `x.toString(10)`\n- string parsing in bases 2-36, `x = new BigFloat32('abc.def', 16)`\n\nwithout ever losing any significant bits. Numbers are immutable in the above operations, so they return a new BigFloat.\nFor efficiency, the following methods instead destructively change the value:\n\n- `x.truncate(limbs)` rounds the fractional digits towards zero, to `limbs * 32` or `limbs * 53` bits.\n- `x.round(digits)` rounds approximately to `digits` decimal places (to enough limbs to hold them).\n\nDivision is deliberately unsupported, because its result is generally inexact.\nPlease multiply by the reciprocal or use rational numbers instead.\nNote that floating point values in numerators and denominators are perfectly cromulent.\nIf you need square roots or transcendental functions, use some other library.\n\nThere are two versions of the class, `BigFloat32` and `BigFloat53` with the same API but completely different internals as follows:\n\n**BigFloat32**\n\n- Arbitrarily long sequence of bits split into 32-bit integers (\"limbs\", effectively digits in base `2 ** 32`),\n  somewhat like in the [GMP](https://gmplib.org/manual/Float-Internals.html) library.\n- The decimal point is a position between limbs, splitting the list of limbs into integer and fractional halves.\n- Dense representation: all bits between the most and least significant are stored.\n  - Optimized for exponents relatively close to zero, so the location of the decimal point is always present in the limb array,\n    even if that introduces otherwise insignificant leading or trailing zero limbs.\n- Precision is only limited by available memory.\n- Uses integer math for best portability.\n- Faster for operations between two arbitrary precision `BigFloat32` objects, slower for converting to / from JavaScript numbers.\n\n**BigFloat53**\n\n- Floating point expansion consisting of an unevaluated sum of components\n  (JavaScript floating point numbers) ordered by increasing magnitude.\n- Multiple representations exist for each number, depending on how bits are split between components\n  (this is transparent: they still compare as equal).\n- Each component can hold 1-53 bits of significand.\n- Sparse representation: components consisting entirely of zeroes are not stored.\n- Precision is limited by exponents representable in IEEE 754.\n  - Binary digits at positions less significant than `2 ** -1074` (smallest double precision denormal) will disappear.\n  - Numbers rounding to `2 ** 1024` or greater will overflow **spectacularly**.\n- Uses error free transformations (see JR Shewchuk.\n  [*Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates*](doc/robustr.pdf),\n  1997).\n  - Requires accurate rounding to nearest with ties to even as specified by EcmaScript 5.1 and up\n    ([section 8.5](https://www.ecma-international.org/ecma-262/5.1/#sec-8.5)).\n- Faster for operations between arbitrary precision `BigFloat53` objects and JavaScript numbers, slower for operations between two arbitrary precision objects.\n\nIn both versions the least significant limb / component is stored first,\nbecause basic algorithms for arithmetic operations progress from the least to most significant digit while propagating carry.\nIf carry causes the output to grow, adding a new limb at the end of the array is faster than adding it in the beginning.\n\nTL;DR: Use `BigFloat32` for long operations between arbitrary precision floats, portability and to avoid under / overflow.\nUse `BigFloat53` for short calculations with many ordinary JavaScript numbers as inputs.\n\nYou may want to test with both to compare their speed and see if you run into overflow\nor any floating point portability issues on mobile platforms.\n\n## Optimization\n\nIn any longer iterated calculations involving multiplication, `truncate` should be called regularly because otherwise significant bits will keep accumulating.\nFor example, squaring a number doubles the number of bits at every step, easily turning an algorithm with linear complexity into a quadratic one\n(both in speed and space).\n\nTo avoid surprises, the basic operations allocate new objects for storing results. A second parameter can be given,\nwith a result `BigFloat` object of the same type (32 or 53). Its contents will be destructively overwritten with the result,\nto save a memory allocation. This avoids garbage collection related slowdowns in longer calculations.\n\nSome care is needed in re-using temporary variables, because inputs cannot be simultaneously used as results:\n\n```TypeScript\nx.add(y, w).sub(z, w)\n```\n\nfails because in the subtraction, `w` is both the subtrahend and the difference.\n\nExisting objects can also be re-initialized with:\n\n- zero, `x.setZero()`\n- new value from a JavaScript number, `x.setValue(123.456)`\n\nAdditionally, `BigFloat53` objects support initialization from results of operations between two JavaScript numbers:\n\n- sum, `x.setSum(12.34, 56.78)`\n- product, `x.setProduct(12.34, 56.78)`\n\nThese use very fast double double arithmetic (error free transformations).\n\n## Speed\n\nIt's fast, see the [Mandelbrot benchmark](http://charto.github.io/bigfloat/). Here's some example results:\n\nNative JavaScript IEEE 754:  \n████████████████████████████████ // ██ 80000 frames per minute\n\n`bigfloat`:  \n████████████████████████████ 141 frames per minute\n\n[bignumber.js](https://github.com/MikeMcl/bignumber.js):  \n██████████ 48 frames per minute\n\n[big.js](https://github.com/MikeMcl/big.js):  \n███████ 35 frames per minute\n\nGetting started\n---\n\n```bash\ngit clone https://github.com/charto/bigfloat.git node_modules/bigfloat\ncd node_modules/bigfloat \u0026\u0026 npm install\ncd ../..\nnode\n```\n\nOR\n\n```bash\nnpm install bigfloat\nnode\n```\n\nTHEN\n\n```js\nx = Math.pow(2, 53);\nconsole.log(x + 1 - x); // Prints 0\n\nBigFloat32 = require('bigfloat').BigFloat32;\nconsole.log(new BigFloat32(x).add(1).sub(x).toString()); // Prints 1\n```\n\n# License\n\n[The MIT License](https://raw.githubusercontent.com/charto/bigfloat/master/LICENSE)\n\nCopyright (c) 2015- BusFaster Ltd\n\nThe paper `doc/robustr.pdf` is copyright JR Shewchuk and licenced as detailed inside under \"About this Report\".\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharto%2Fbigfloat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcharto%2Fbigfloat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcharto%2Fbigfloat/lists"}