{"id":13620593,"url":"https://github.com/PaulRBerg/prb-math","last_synced_at":"2025-04-14T22:32:00.687Z","repository":{"id":37839749,"uuid":"351127674","full_name":"PaulRBerg/prb-math","owner":"PaulRBerg","description":"Solidity library for advanced fixed-point math","archived":false,"fork":false,"pushed_at":"2025-04-09T09:12:43.000Z","size":9752,"stargazers_count":927,"open_issues_count":22,"forks_count":130,"subscribers_count":10,"default_branch":"main","last_synced_at":"2025-04-10T19:51:21.945Z","etag":null,"topics":["decentralized-finance","defi","ethereum","evm","library","math","smart-contracts","solidity"],"latest_commit_sha":null,"homepage":"","language":"Solidity","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/PaulRBerg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"custom":"https://3cities.xyz/#/pay?c=CAESFAKY9DMuOFdjE4Wzl2YyUFipPiSfIgICATICCAJaFURvbmF0aW9uIHRvIFBhdWwgQmVyZw","github":"PaulRBerg"}},"created_at":"2021-03-24T15:19:27.000Z","updated_at":"2025-04-10T06:55:07.000Z","dependencies_parsed_at":"2023-11-15T11:27:13.001Z","dependency_job_id":"67edf1d3-30a1-461a-a40e-777307683d27","html_url":"https://github.com/PaulRBerg/prb-math","commit_stats":{"total_commits":495,"total_committers":14,"mean_commits":"35.357142857142854","dds":0.1656565656565656,"last_synced_commit":"9e7e21e6f48d60802cbf100fae392f36e0600bbd"},"previous_names":["hifi-finance/prb-math"],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaulRBerg%2Fprb-math","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaulRBerg%2Fprb-math/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaulRBerg%2Fprb-math/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PaulRBerg%2Fprb-math/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PaulRBerg","download_url":"https://codeload.github.com/PaulRBerg/prb-math/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248971883,"owners_count":21191679,"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":["decentralized-finance","defi","ethereum","evm","library","math","smart-contracts","solidity"],"created_at":"2024-08-01T21:00:57.470Z","updated_at":"2025-04-14T22:32:00.673Z","avatar_url":"https://github.com/PaulRBerg.png","language":"Solidity","readme":"# PRBMath [![GitHub Actions][gha-badge]][gha] [![Foundry][foundry-badge]][foundry] [![License: MIT][license-badge]][license]\n\n[gha]: https://github.com/PaulRBerg/prb-math/actions\n[gha-badge]: https://github.com/PaulRBerg/prb-math/actions/workflows/ci.yml/badge.svg\n[foundry]: https://getfoundry.sh/\n[foundry-badge]: https://img.shields.io/badge/Built%20with-Foundry-FFDB1C.svg\n[license]: https://opensource.org/licenses/MIT\n[license-badge]: https://img.shields.io/badge/License-MIT-blue.svg\n\n**Solidity library for advanced fixed-point math** that operates with signed 59.18-decimal fixed-point and unsigned 60.18-decimal fixed-point numbers.\nThe name of the number format comes from the integer part having up to 59 digits for signed numbers and 60 digits for unsigned numbers, while the\nfractional part has up to 18 decimals. The numbers are bound by the minimum and the maximum values permitted by the Solidity types int256 and uint256.\n\n- Operates with signed and unsigned denary fixed-point numbers, with 18 trailing decimals\n- Offers advanced math functions like logarithms, exponentials, powers and square roots\n- Provides type safety via user-defined value types\n- Gas efficient, but still user-friendly\n- Ergonomic developer experience thanks to using free functions instead of libraries\n- Bakes in overflow-safe multiplication and division via `mulDiv`\n- Reverts with custom errors instead of reason strings\n- Well-documented with NatSpec comments\n- Built and tested with Foundry\n\nI created this because I wanted a fixed-point math library that is at the same time intuitive, efficient and safe. I looked at\n[ABDKMath64x64](https://github.com/abdk-consulting/abdk-libraries-solidity), which is fast, but it uses binary numbers which are counter-intuitive and\nnon-familiar to humans. Then, I looked at [Fixidity](https://github.com/CementDAO/Fixidity), which operates with denary numbers and has wide\nprecision, but is slow and susceptible to phantom overflow. Finally, I looked at [Solmate](https://github.com/transmissions11/solmate), which checks\nall the boxes mentioned thus far, but it doesn't offer type safety.\n\n## Install\n\n### Node.js\n\nThis is the recommended approach.\n\nInstall PRBMath using your favorite package manager, e.g., with Bun:\n\n```shell\nbun add @prb/math\n```\n\nThen, if you are using Foundry, you need to add this to your `remappings.txt` file:\n\n```text\n@prb/math/=node_modules/@prb/math/\n```\n\n### Git Submodules\n\nThis installation method is not recommended, but it is available for those who prefer it.\n\nFirst, install the submodule using Forge:\n\n```shell\nforge install --no-commit PaulRBerg/prb-math@release-v4\n```\n\nYour `.gitmodules` file should now contain the following entry:\n\n```toml\n[submodule \"lib/prb-math\"]\n  branch = \"release-v4\"\n  path = \"lib/prb-math\"\n  url = \"https://github.com/PaulRBerg/prb-math\"\n```\n\nFinally, add this to your `remappings.txt` file:\n\n```text\n@prb/math/=lib/prb-math/\n```\n\n## Usage\n\nThere are two user-defined value types:\n\n1. SD59x18 (signed)\n2. UD60x18 (unsigned)\n\nIf you don't know what a user-defined value type is, check out this [blog post](https://blog.soliditylang.org/2021/09/27/user-defined-value-types/).\n\nIf you don't need negative numbers, there's no point in using the signed flavor `SD59x18`. The unsigned flavor `UD60x18` is more gas efficient.\n\nNote that PRBMath is not a library in the Solidity [sense](https://docs.soliditylang.org/en/v0.8.17/contracts.html#libraries). It's just a collection\nof free functions.\n\n### Importing\n\nIt is recommended that you import PRBMath using specific symbols. Importing full files can result in Solidity complaining about duplicate definitions\nand static analyzers like Slither erroring, especially as repos grow and have more dependencies with overlapping names.\n\n```solidity\npragma solidity \u003e=0.8.19;\n\nimport { SD59x18 } from \"@prb/math/src/SD59x18.sol\";\nimport { UD60x18 } from \"@prb/math/src/UD60x18.sol\";\n```\n\nAny function that is not available in the types directly has to be imported explicitly. Here's an example for the `sd` and the `ud` functions:\n\n```solidity\npragma solidity \u003e=0.8.19;\n\nimport { SD59x18, sd } from \"@prb/math/src/SD59x18.sol\";\nimport { UD60x18, ud } from \"@prb/math/src/UD60x18.sol\";\n```\n\nNote that PRBMath can only be used in Solidity v0.8.19 and above.\n\n### SD59x18\n\n```solidity\n// SPDX-License-Identifier: UNLICENSED\npragma solidity \u003e=0.8.19;\n\nimport { SD59x18, sd } from \"@prb/math/src/SD59x18.sol\";\n\ncontract SignedConsumer {\n  /// @notice Calculates 5% of the given signed number.\n  /// @dev Try this with x = 400e18.\n  function signedPercentage(SD59x18 x) external pure returns (SD59x18 result) {\n    SD59x18 fivePercent = sd(0.05e18);\n    result = x.mul(fivePercent);\n  }\n\n  /// @notice Calculates the binary logarithm of the given signed number.\n  /// @dev Try this with x = 128e18.\n  function signedLog2(SD59x18 x) external pure returns (SD59x18 result) {\n    result = x.log2();\n  }\n}\n```\n\n### UD60x18\n\n```solidity\n// SPDX-License-Identifier: UNLICENSED\npragma solidity \u003e=0.8.19;\n\nimport { UD60x18, ud } from \"@prb/math/src/UD60x18.sol\";\n\ncontract UnsignedConsumer {\n  /// @notice Calculates 5% of the given signed number.\n  /// @dev Try this with x = 400e18.\n  function unsignedPercentage(UD60x18 x) external pure returns (UD60x18 result) {\n    UD60x18 fivePercent = ud(0.05e18);\n    result = x.mul(fivePercent);\n  }\n\n  /// @notice Calculates the binary logarithm of the given signed number.\n  /// @dev Try this with x = 128e18.\n  function unsignedLog2(UD60x18 x) external pure returns (UD60x18 result) {\n    result = x.log2();\n  }\n}\n```\n\n## Features\n\nBecause there's significant overlap between the features available in SD59x18 and UD60x18, there is only one table per section. If in doubt, refer to\nthe source code, which is well-documented with NatSpec comments.\n\n### Mathematical Functions\n\n| Name    | Operator | Description                                      |\n| ------- | -------- | ------------------------------------------------ |\n| `abs`   | N/A      | Absolute value                                   |\n| `avg`   | N/A      | Arithmetic average                               |\n| `ceil`  | N/A      | Smallest whole number greater than or equal to x |\n| `div`   | `/`      | Fixed-point division                             |\n| `exp`   | N/A      | Natural exponential e^x                          |\n| `exp2`  | N/A      | Binary exponential 2^x                           |\n| `floor` | N/A      | Greatest whole number less than or equal to x    |\n| `frac`  | N/A      | Fractional part                                  |\n| `gm`    | N/A      | Geometric mean                                   |\n| `inv`   | N/A      | Inverse 1÷x                                      |\n| `ln`    | N/A      | Natural logarithm ln(x)                          |\n| `log10` | N/A      | Common logarithm log10(x)                        |\n| `log2`  | N/A      | Binary logarithm log2(x)                         |\n| `mul`   | `*`      | Fixed-point multiplication                       |\n| `pow`   | N/A      | Power function x^y                               |\n| `powu`  | N/A      | Power function x^y with y simple integer         |\n| `sqrt`  | N/A      | Square root                                      |\n\n### Adjacent Value Types\n\nPRBMath provides adjacent value types that serve as abstractions over other vanilla types:\n\n| Value Type | Underlying Type |\n| ---------- | --------------- |\n| `SD1x18`   | int64           |\n| `SD21x18`  | int128          |\n| `UD2x18`   | uint64          |\n| `UD21x18`  | uint128         |\n\nThese are useful if you want to save gas by using a lower bit width integer, e.g., in a struct.\n\nNote that these types don't have any mathematical functionality. To do math with them, you will have to unwrap them into a simple integer and then to\nthe core types `SD59x18` and `UD60x18`.\n\n### Casting Functions\n\nAll PRBMath types have casting functions to and from all other types, including a few basic types like `uint128` and `uint40`.\n\n| Name          | Description               |\n| ------------- | ------------------------- |\n| `intoSD1x18`  | Casts a number to SD1x18  |\n| `intoSD59x18` | Casts a number to SD59x18 |\n| `intoUD2x18`  | Casts a number to UD2x18  |\n| `intoUD60x18` | Casts a number to UD60x18 |\n| `intoUint256` | Casts a number to uint256 |\n| `intoUint128` | Casts a number to uint128 |\n| `intoUint40`  | Casts a number to uint40  |\n| `sd1x18`      | Alias for `SD1x18.wrap`   |\n| `sd59x18`     | Alias for `SD59x18.wrap`  |\n| `ud2x18`      | Alias for `UD2x18.wrap`   |\n| `ud60x18`     | Alias for `UD60x18.wrap`  |\n\n### Conversion Functions\n\nThe difference between \"conversion\" and \"casting\" is that conversion functions multiply or divide the inputs, whereas casting functions simply cast\nthem.\n\n| Name               | Description                                                           |\n| ------------------ | --------------------------------------------------------------------- |\n| `convert(SD59x18)` | Converts an SD59x18 number to a simple integer by dividing it by 1e18 |\n| `convert(UD60x18)` | Converts a UD60x18 number to a simple integer by dividing it by 1e18  |\n| `convert(int256)`  | Converts a simple integer to SD59x18 by multiplying it by 1e18        |\n| `convert(uint256)` | Converts a simple integer to UD60x18 type by multiplying it by 1e18   |\n\n### Helper Functions\n\nIn addition to offering mathematical, casting, and conversion functions, PRBMath provides numerous helper functions for user-defined value types:\n\n| Name           | Operator | Description               |\n| -------------- | -------- | ------------------------- |\n| `add`          | `+`      | Checked addition          |\n| `and`          | `\u0026`      | Logical AND               |\n| `eq`           | `==`     | Equality                  |\n| `gt`           | `\u003e`      | Greater than operator     |\n| `gte`          | `\u003e=`     | Greater than or equal to  |\n| `isZero`       | N/A      | Check if a number is zero |\n| `lshift`       | N/A      | Bitwise left shift        |\n| `lt`           | `\u003c`      | Less than                 |\n| `lte`          | `\u003c=`     | Less than or equal to     |\n| `mod`          | `%`      | Modulo                    |\n| `neq`          | `!=`     | Not equal operator        |\n| `not`          | `~`      | Negation operator         |\n| `or`           | `\\|`     | Logical OR                |\n| `rshift`       | N/A      | Bitwise right shift       |\n| `sub`          | `-`      | Checked subtraction       |\n| `unary`        | `-`      | Checked unary             |\n| `uncheckedAdd` | N/A      | Unchecked addition        |\n| `uncheckedSub` | N/A      | Unchecked subtraction     |\n| `xor`          | `^`      | Exclusive or (XOR)        |\n\nThese helpers are designed to streamline basic operations such as addition and equality checks, eliminating the need to constantly unwrap and re-wrap\nvariables. However, it is important to be aware that utilizing these functions may result in increased gas costs compared to unwrapping and directly\nusing the vanilla types.\n\n```solidity\n// SPDX-License-Identifier: UNLICENSED\npragma solidity \u003e=0.8.19;\n\nimport { UD60x18, ud } from \"@prb/math/src/UD60x18.sol\";\n\nfunction addRshiftEq() pure returns (bool result) {\n  UD60x18 x = ud(1e18);\n  UD60x18 y = ud(3e18);\n  y = y.add(x); // also: y = y + x\n  y = y.rshift(2);\n  result = x.eq(y); // also: y == x\n}\n\n```\n\n### Assertions\n\nPRBMath comes with typed assertions that you can use for writing tests with [PRBTest](https://github.com/PaulRBerg/prb-test), which is based on\nFoundry. This is useful if, for example, you would like to assert that two UD60x18 numbers are equal.\n\n```solidity\npragma solidity \u003e=0.8.19;\n\nimport { UD60x18, ud } from \"@prb/math/src/UD60x18.sol\";\nimport { Assertions as PRBMathAssertions } from \"@prb/math/test/Assertions.sol\";\nimport { PRBTest } from \"@prb/math/src/test/PRBTest.sol\";\n\ncontract MyTest is PRBTest, PRBMathAssertions {\n  function testAdd() external {\n    UD60x18 x = ud(1e18);\n    UD60x18 y = ud(2e18);\n    UD60x18 z = ud(3e18);\n    assertEq(x.add(y), z);\n  }\n}\n```\n\n## Gas Efficiency\n\nPRBMath is faster than ABDKMath for `abs`, `exp`, `exp2`, `gm`, `inv`, `ln`, `log2`, but it is slower than ABDKMath for `avg`, `div`, `mul`, `powu`\nand `sqrt`.\n\nThe main reason why PRBMath lags behind ABDKMath's `mul` and `div` functions is that it operates with 256-bit word sizes, and so it has to account for\npossible intermediary overflow. ABDKMath, on the other hand, operates with 128-bit word sizes.\n\n**Note**: I did not find a good way to automatically generate gas reports for PRBMath. See the\n[#134](https://github.com/PaulRBerg/prb-math/discussions/134) discussion for more details about this issue.\n\n### PRBMath\n\nGas estimations based on the [v2.0.1](https://github.com/PaulRBerg/prb-math/releases/tag/v2.0.1) and the\n[v3.0.0](https://github.com/PaulRBerg/prb-math/releases/tag/v3.0.0) releases.\n\n| SD59x18 | Min | Max   | Avg  |     | UD60x18 | Min  | Max   | Avg  |\n| ------- | --- | ----- | ---- | --- | ------- | ---- | ----- | ---- |\n| abs     | 68  | 72    | 70   |     | n/a     | n/a  | n/a   | n/a  |\n| avg     | 95  | 105   | 100  |     | avg     | 57   | 57    | 57   |\n| ceil    | 82  | 117   | 101  |     | ceil    | 78   | 78    | 78   |\n| div     | 431 | 483   | 451  |     | div     | 205  | 205   | 205  |\n| exp     | 38  | 2797  | 2263 |     | exp     | 1874 | 2742  | 2244 |\n| exp2    | 63  | 2678  | 2104 |     | exp2    | 1784 | 2652  | 2156 |\n| floor   | 82  | 117   | 101  |     | floor   | 43   | 43    | 43   |\n| frac    | 23  | 23    | 23   |     | frac    | 23   | 23    | 23   |\n| gm      | 26  | 892   | 690  |     | gm      | 26   | 893   | 691  |\n| inv     | 40  | 40    | 40   |     | inv     | 40   | 40    | 40   |\n| ln      | 463 | 7306  | 4724 |     | ln      | 419  | 6902  | 3814 |\n| log10   | 104 | 9074  | 4337 |     | log10   | 503  | 8695  | 4571 |\n| log2    | 377 | 7241  | 4243 |     | log2    | 330  | 6825  | 3426 |\n| mul     | 455 | 463   | 459  |     | mul     | 219  | 275   | 247  |\n| pow     | 64  | 11338 | 8518 |     | pow     | 64   | 10637 | 6635 |\n| powu    | 293 | 24745 | 5681 |     | powu    | 83   | 24535 | 5471 |\n| sqrt    | 140 | 839   | 716  |     | sqrt    | 114  | 846   | 710  |\n\n### ABDKMath64x64\n\nGas estimations based on the v3.0 release of ABDKMath. See my [abdk-gas-estimations](https://github.com/PaulRBerg/abdk-gas-estimations) repo.\n\n| Method | Min  | Max  | Avg  |\n| ------ | ---- | ---- | ---- |\n| abs    | 88   | 92   | 90   |\n| avg    | 41   | 41   | 41   |\n| div    | 168  | 168  | 168  |\n| exp    | 77   | 3780 | 2687 |\n| exp2   | 77   | 3600 | 2746 |\n| gavg   | 166  | 875  | 719  |\n| inv    | 157  | 157  | 157  |\n| ln     | 7074 | 7164 | 7126 |\n| log2   | 6972 | 7062 | 7024 |\n| mul    | 111  | 111  | 111  |\n| pow    | 303  | 4740 | 1792 |\n| sqrt   | 129  | 809  | 699  |\n\n## Contributing\n\nFeel free to dive in! [Open](https://github.com/PaulRBerg/prb-math/issues/new) an issue,\n[start](https://github.com/PaulRBerg/prb-math/discussions/new) a discussion or submit a PR.\n\n### Pre Requisites\n\nYou will need the following software on your machine:\n\n- [Git](https://git-scm.com/downloads)\n- [Foundry](https://github.com/foundry-rs/foundry)\n- [Node.Js](https://nodejs.org/en/download/)\n- [Bun](https://bun.sh)\n\nIn addition, familiarity with [Solidity](https://soliditylang.org/) is requisite.\n\n### Set Up\n\nClone this repository including submodules:\n\n```sh\n$ git clone --recurse-submodules -j8 git@github.com:PaulRBerg/prb-math.git\n```\n\nThen, inside the project's directory, run this to install the Node.js dependencies:\n\n```sh\n$ bun install\n```\n\nNow you can start making changes.\n\n### Syntax Highlighting\n\nYou will need the following VSCode extensions:\n\n- [hardhat-solidity](https://marketplace.visualstudio.com/items?itemName=NomicFoundation.hardhat-solidity)\n- [vscode-tree-language](https://marketplace.visualstudio.com/items?itemName=CTC.vscode-tree-extension)\n\n## Security\n\nThe codebase has undergone audits by leading security experts from Cantina and Certora. For a comprehensive list of all audits conducted, see the\n[SECURITY](./SECURITY.md) file.\n\n### Caveat Emptor\n\nThis is experimental software and is provided on an \"as is\" and \"as available\" basis. I do not give any warranties and will not be liable for any\nloss, direct or indirect through continued use of this codebase.\n\n### Contact\n\nIf you discover any bugs or security issues, please report them via [Telegram](https://t.me/PaulRBerg).\n\n## Acknowledgments\n\n- Mikhail Vladimirov for the insights he shared in the [Math in Solidity](https://medium.com/coinmonks/math-in-solidity-part-1-numbers-384c8377f26d)\n  article series.\n- Remco Bloemen for his work on [overflow-safe multiplication and division](https://xn--2-umb.com/21/muldiv/), and for responding to the questions I\n  asked him while developing the library.\n- Everyone who has contributed a PR to this repository.\n\n## License\n\nThis project is licensed under MIT.\n","funding_links":["https://3cities.xyz/#/pay?c=CAESFAKY9DMuOFdjE4Wzl2YyUFipPiSfIgICATICCAJaFURvbmF0aW9uIHRvIFBhdWwgQmVyZw","https://github.com/sponsors/PaulRBerg"],"categories":["Libraries","Solidity"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPaulRBerg%2Fprb-math","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPaulRBerg%2Fprb-math","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPaulRBerg%2Fprb-math/lists"}