Solidity doesn't have fixed-point, floating-point, or double types. What solutions are there to handle the cases where we really have to represent numbers as percentages or fractions?
Asked
Active
Viewed 7,404 times
1 Answers
27
Disclaimer: I am the author of PRBMath.
Fixed-Point
- PRBMath
- signed and unsigned denary numbers with 18 decimals of precisions
- offers advanced math functions (logs, exp, pow, etc.)
- provides type safety via user defined value types
- gas efficient, but still user-friendly
- ergonomic developer experience thanks to using free functions instead of libraries
- ABDKMath64x64
- binary numbers with 2^64 precision
- offers advanced math functions (logs, exp, pow, etc.)
- ultra gas efficient (see this praise)
- Fixidity
- denary numbers with arbitrary number of decimals
- offers advanced math functions (logs, exp, pow, etc.)
- slower than 64.64, but with a more palatable API
- used by CementDAO, PoolTogether and Celo
- Exponential
- denary numbers with 18 decimals of precision
- minimal features, doesn't offer advanced math functions
- developed by Compound.Finance
- DecimalMath
- denary numbers with 27 decimals of precision
- minimal features, doesn't offer advanced math functions
- uses ABIEncoderV2 (experimental in Solidity 0.7 and below)
- DSMath
Floating-Point
- ABDKMathQuad
- developed by the same company as 64.64
- Bankex
- IEEE-754 octuple precision floating-point
Addenda
- There's an open issue in the solidity repo for adding native support for fixed-point types
- Mikhail Vladimirov's Math in Solidity blog series is a fantastic resource to learn about how advanced math functions can be implemented in Solidity v0.6 and lower
- There are more math libraries mentioned in this OpenZeppelin forum post
Paul Razvan Berg
- 17,902
- 6
- 73
- 143
brittlest, which gave me:having hardness and rigidity but little tensile strength. Now I have to google-translate pretty much every one of the words in that... – goodvibration May 27 '20 at 20:52