|
| 1 | +--- |
| 2 | +status: accepted |
| 3 | +flip: 341 |
| 4 | +authors: Supun Setunga (supun.setunga@flowfoundation.org) |
| 5 | +sponsor: Dieter Shirley (dete@flowfoundation.com) |
| 6 | +updated: 2025-08-29 |
| 7 | +--- |
| 8 | + |
| 9 | +# FLIP 341: Add 128-bit Fixed-point Types to Cadence |
| 10 | + |
| 11 | +## Objective |
| 12 | + |
| 13 | +The objective is to add a set of 128-bit wide fixed-point types, `Fix128` and `UFix128` to Cadence. |
| 14 | + |
| 15 | +## Motivation |
| 16 | + |
| 17 | +Cadence currently only supports 64-bit wide decimal fixed-point types, `Fix64` and `UFix64`, |
| 18 | +having the ranges `-92233720368.54775808` through `92233720368.54775807` and `0.0` through `184467440737.09551615` |
| 19 | +respectively. |
| 20 | + |
| 21 | +However, there could be requirements to have a much higher precision than what `Fix64` and `UFix64` provide. |
| 22 | +For example, applications that handle financial data would require higher precision for arithmetic operations. |
| 23 | +Though this can be achieved with a custom fixed-point implementation using integers (128-bit length, 256-bit length, |
| 24 | +or even unbounded integers), it is not only a lot of work for developers to implement such a type, |
| 25 | +but also would be very inefficient. |
| 26 | + |
| 27 | +## User Benefit |
| 28 | + |
| 29 | +Developers can use fixed-point values in Cadence for high-precision arithmetics, efficiently and conveniently. |
| 30 | + |
| 31 | +## Design Proposal |
| 32 | + |
| 33 | +Add `Fix128` and `UFix128` to Cadence. |
| 34 | + |
| 35 | +The proposal is to use a scale of 24, and therefore a scaling factor of 1e-24. |
| 36 | + |
| 37 | +Other blockchain ecosystems (most notably Ethereum) frequently use scale factors (usually called "decimals", per ERC-20) |
| 38 | +of 18 (used by ETH itself), 24 (used for internal calculations in MakerDAO), and 27 (used internally by Compound and Aave |
| 39 | +for interest calculations). |
| 40 | +Although 27 is the most commonly used value internally by defi protocols, the value of 24 has a precedent in Maker, and |
| 41 | +introduces a type that can hold values in the trillions (extreme, but plausible values for very large financial calculations). |
| 42 | +Note that all `UFix64` and `Fix64` values can be converted to the equivalent 128-bit types without any loss of precision or range. |
| 43 | + |
| 44 | +A scaling factor of 24 provides a very high precision for fractional values, while also leaving large enough upper and |
| 45 | +lower bounds sufficient for most real-world use cases (which typically will involve internal calculations that convert |
| 46 | +back to the 64-bit types). |
| 47 | + |
| 48 | +The new fixed-point type can accommodate values in the following ranges: |
| 49 | +- `Fix128`: `-170141183460469.231731687303715884105728` through `170141183460469.231731687303715884105727` |
| 50 | +- `UFix128`: `0.0` through `340282366920938.463463374607431768211455` |
| 51 | + |
| 52 | +### Drawbacks |
| 53 | + |
| 54 | +None. |
| 55 | + |
| 56 | +### Performance Implications |
| 57 | + |
| 58 | +None. |
| 59 | + |
| 60 | +### Dependencies |
| 61 | + |
| 62 | +None. |
| 63 | + |
| 64 | +### Engineering Impact |
| 65 | + |
| 66 | +Implementing a new and efficient fixed-point representation is non-trivial. |
| 67 | + |
| 68 | +### Compatibility |
| 69 | + |
| 70 | +Given this is a new feature addition, there's no impact on backward compatibility. |
| 71 | + |
| 72 | +### User Impact |
| 73 | + |
| 74 | +Given this is a new feature addition, there's no impact for existing users. |
| 75 | + |
| 76 | +## Related Issues |
| 77 | + |
| 78 | +None. |
| 79 | + |
| 80 | +## Implementation |
| 81 | +- `Fix128` - https://github.com/onflow/cadence/pull/4131 |
| 82 | +- `UFix128` - https://github.com/onflow/cadence/pull/4147 |
| 83 | + |
| 84 | +## Prior Art |
| 85 | + |
| 86 | +Most programing languages do not have built-in types for 128-bit fixed-point values, but are provided either |
| 87 | +as a standard library or are available as third-party libraries. e.g: |
| 88 | +- Python: https://docs.python.org/3/library/decimal.html |
| 89 | +- Rust: https://docs.rs/fixed/latest/fixed/index.html |
| 90 | + |
0 commit comments