{"id":13817067,"url":"https://github.com/polywrap/as-bignumber","last_synced_at":"2025-04-13T00:55:25.545Z","repository":{"id":43490258,"uuid":"460920343","full_name":"polywrap/as-bignumber","owner":"polywrap","description":"An AssemblyScript class for math with arbitrary-precision decimal and integer numbers","archived":false,"fork":false,"pushed_at":"2022-07-18T16:28:30.000Z","size":268,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-13T00:55:12.446Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/polywrap.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":".github/CODEOWNERS","security":null,"support":null}},"created_at":"2022-02-18T16:13:52.000Z","updated_at":"2024-02-24T23:15:30.000Z","dependencies_parsed_at":"2022-09-15T00:13:38.532Z","dependency_job_id":null,"html_url":"https://github.com/polywrap/as-bignumber","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/polywrap%2Fas-bignumber","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polywrap%2Fas-bignumber/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polywrap%2Fas-bignumber/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polywrap%2Fas-bignumber/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/polywrap","download_url":"https://codeload.github.com/polywrap/as-bignumber/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650438,"owners_count":21139672,"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-08-04T06:00:32.812Z","updated_at":"2025-04-13T00:55:25.522Z","avatar_url":"https://github.com/polywrap.png","language":"TypeScript","funding_links":[],"categories":["Packages"],"sub_categories":[],"readme":"# BigNumber\nBigNumber is an AssemblyScript class for math with arbitrary-precision decimal and integer numbers\n\n## Features\n\n- Fast arithmetic operations\n- Lightweight\n- Immutable instances\n- Core operations thoroughly tested\n\n## Getting Started\n\n### Installation\n`npm install as-bignumber`  \nor  \n`yarn add as-bignumber`\n\n### Quick start\n\n```typescript\nimport { BigNumber } from \"as-fraction\"\n\n// generic constructor supports string, BigInt, f32, f64, and all native integer types\nconst a: BigNumber = BigNumber.from(\"1.93\");\n// construct from string\nconst b: BigNumber = BigNumber.fromString(\"1.93\");\n// construct from BigInt\nconst c: BigNumber = BigNumber.fromBigInt(BigInt.from(\"3\"));\n// construct from fraction components\nconst d: BigNumber = BigNumber.fromFraction(BigInt.from(\"1\"), BigInt.from(\"3\"))\n\n// arithmetic (operator overloads: +, -, *, /, **)\nconst sum: BigNumber = a.add(b);\nconst difference: BigNumber = a.sub(b);\nconst product: BigNumber = a.mul(b);\nconst quotient: BigNumber = a.div(b);\nconst exponential: BigNumber = a.pow(3);\nconst squared: BigNumber = a.square();\nconst squareRoot: BigNumber = a.sqrt();\n\n// comparison operations (operator overloads: ==, !=, \u003c, \u003c=, \u003e, \u003e=)\nconst isEqual: boolean = a.eq(b);\nconst isNotEqual: boolean = a.ne(b);\nconst isLessThan: boolean = a.lt(b);\nconst isLessThanOrEqualTo: boolean = a.lte(b);\nconst isGreaterThan: boolean = a.gt(b);\nconst isGreaterThanOrEqualTo: boolean = a.gte(b);\n\n// binary arithmetic and comparison operators also have static implementations\nconst staticProduct: BigNumber = BigNumber.mul(a, b);\nconst staticIsEqual: boolean = BigNumber.eq(a, b);\n\n// instantiate new copy, absolute value, opposite, or reciprocal\nconst sameNumber: BigNumber = a.copy();\nconst positiveNumber: BigNumber = a.abs();\nconst oppositeSign: BigNumber = a.opposite();\nconst reciprocal: BigNumber = a.reciprocal();\n\n// convenience functions\nconst isNegative: boolean = a.isNegative;\nconst isInteger: boolean = a.isInteger;\nconst isZeroNumber: boolean = a.isZero();\nconst one: BigNumber = BigNumber.ONE;\nconst half: BigNumber = BigNumber.HALF;\n\n// string output\nconst toString: string = a.toString();\nconst toSignificant: string = a.toSignificant(digits, rounding);\nconst toFixed: string = a.toFixed(places, rounding);\n\n// type conversions\nconst toBigNumber: BigNumber = a.toBigNumber();\nconst toBigInt: BigInt = a.toBigInt();\nconst toFloat: f64 = a.toFloat64();\n\nexport enum Rounding {\n  UP, // Rounding mode to round away from zero.\n  DOWN, // Rounding mode to round towards zero.\n  CEIL, // Rounding mode to round towards positive infinity.\n  FLOOR, // Rounding mode to round towards negative infinity.\n  HALF_UP, // Rounding mode to round towards \"nearest neighbor\" unless both neighbors are equidistant, in which case round up.\n  HALF_DOWN, // Rounding mode to round towards \"nearest neighbor\" unless both neighbors are equidistant, in which case round down.\n  HALF_EVEN, // Rounding mode to round towards the \"nearest neighbor\" unless both neighbors are equidistant, in which case, round towards the even neighbor.\n  NONE, // Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.\n}\n```\n### Rounding and Precision\n\nBecause some numbers are irrational or have infinite digits, rounding is sometimes necessary. Numbers are rounded to a \nspecific number of digits, which we call 'precision'. For example, the number 123.45 has five digits, and therefore has \na precision of 5.\n\nBy default, numbers are assigned a maximum precision of 155 for some operations. When a result of one of these \noperations would exceed 155 digits, it is rounded to the best 155 digits according to the default or specified rounding \nrule. The magnitude of a number can still exceed that of a 155 digit integer due to exponential notation.\n\nArithmetic operations--and other methods that may require rounding--accept optional arguments to specify the precision \nand rounding mode for the result of the operation. \n\nThe default precision and rounding mode can be changed using static methods.\n\nA precision less than or equal to 0 indicates that the that number has exact precision, or that the result of an \narithmetic operation should have exact precision. This can cause operations to throw an exception when a result would \nhave infinite digits.\n\n```typescript\n// rounding modes\nexport enum Rounding {\n  UP, // Rounding mode to round away from zero.\n  DOWN, // Rounding mode to round towards zero.\n  CEIL, // Rounding mode to round towards positive infinity.\n  FLOOR, // Rounding mode to round towards negative infinity.\n  HALF_UP, // Rounding mode to round towards \"nearest neighbor\" unless both neighbors are equidistant, in which case round up.\n  HALF_DOWN, // Rounding mode to round towards \"nearest neighbor\" unless both neighbors are equidistant, in which case round down.\n  HALF_EVEN, // Rounding mode to round towards the \"nearest neighbor\" unless both neighbors are equidistant, in which case, round towards the even neighbor.\n  NONE, // Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.\n}\n\n// many methods accept arguments that adjust precision and rounding\nconst precision: i32 = 250;\nconst rounding: Rounding = Rounding.FLOOR;\nconst quotient: BigNumber = a.div(b, precision, rounding);\n\n// default precision and rounding can be changed\nBigNumber.DEFAULT_PRECISION = 155;\nBigNumber.DEFAULT_ROUNDING = Rounding.HALF_UP;\n```\n\n## Contributing  \n\n### Build  \n`yarn build`  \n\n### Test  \n`yarn test`  \n\n### Lint\n`yarn lint`\n\nTo autofix lint errors:\n`yarn lint:fix`\n\n## Handling integer numbers\n\nIf you need to work with arbitrarily large integers, check out as-bigint: https://github.com/polywrap/as-bigint. The BigInt class facilitates high-performance integer arithmetic.\n\n## Handling fractions\n\nIf you need to work with numbers represented as fractions, check out as-fraction: https://github.com/polywrap/as-fraction. The Fraction class is built on top of BigInt for high-performance fraction arithmetic.\n\n## Acknowledgements\n\nPolywrap developed BigNumber to use in the development tools we produce for fast, language-agnostic decentralized API development. Polywrap allows developers to interact with any web3 protocol from any language, making between-protocol composition easy. Learn more at https://polywrap.io.\n\nas-BigNumber was influenced by [Java's BigDecimal class](https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/math/BigDecimal.java) and the [as-big package](https://github.com/ttulka/as-big).\n\n## Contact\nPlease create an issue in this repository or email kris@dorg.tech\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolywrap%2Fas-bignumber","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpolywrap%2Fas-bignumber","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolywrap%2Fas-bignumber/lists"}