Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(math): Upstream GDA based decimal type #21982

Draft
wants to merge 33 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d16bd19
update dec w benchmark testing
samricotta Apr 18, 2024
7cd5eed
fix version
samricotta Apr 18, 2024
312547c
Update go.mod
samricotta Apr 18, 2024
06fe84c
Create 18-decimal-handling.md
samricotta Apr 24, 2024
e155efe
Update 18-decimal-handling.md
samricotta Apr 30, 2024
b5d9e32
update tests and convert func
samricotta May 2, 2024
3375841
Update 18-decimal-handling.md
samricotta May 2, 2024
d7eacf0
Refactor constructors
alpe May 6, 2024
2b4a8bd
Add more numbers to quo benchmark
alpe May 14, 2024
7132c1b
Apd v3 + sum benchmark
alpe May 14, 2024
94307d1
More benchs
alpe May 14, 2024
53bb505
Marshal/unmarshal
alpe May 14, 2024
166eb0d
Fix test
alpe May 31, 2024
f840cfe
x
alpe Jun 4, 2024
a050988
ip
samricotta Jun 5, 2024
99263b3
wip
samricotta Jun 5, 2024
d5e82d5
quo, quo exact, table tests, multiply tests
samricotta Jun 6, 2024
b6761ba
feat(math): Testing Sub for GDA Dec Type (#20626)
samricotta Jun 17, 2024
7dfeaf5
feat(math): Upstream GDA based decimal type - Add function coverage (…
samricotta Jun 24, 2024
e5cd8cf
feat(math): Upstream GDA based decimal type testing (#20763)
samricotta Jul 3, 2024
e8ef12a
feat(math): Upstream GDA based decimal type - errors (#20827)
samricotta Jul 3, 2024
092a37c
Fix tests and minor refactoring (#20861)
alpe Jul 3, 2024
f77cb17
feat(math): Upstream GDA based decimal type (docs) (#20950)
samricotta Jul 18, 2024
2cfe465
Update 18-decimal-handling.md
samricotta Jul 24, 2024
7cae678
Minor updates
alpe Sep 30, 2024
5c272a6
Merge branch 'main' into alex/sam/dec-type
alpe Sep 30, 2024
3e225dd
go mod tidy all
alpe Sep 30, 2024
68d6dd4
Review comment
alpe Sep 30, 2024
cf6dead
Rename legacy dec files
alpe Sep 30, 2024
8f7ea69
Merge branch 'main' into alex/sam/dec-type
alpe Sep 30, 2024
ea0c5e9
Merge branch 'main' into alex/sam/dec-type
alpe Oct 1, 2024
8448b1e
Linter only
alpe Oct 1, 2024
f735f6d
Merge branch 'main' into alex/sam/dec-type
alpe Oct 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions docs/build/building-modules/18-decimal-handling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
sidebar_position: 1
---
# Decimal Handling in Cosmos SDK

## Introduction

In the Cosmos SDK, there are two types of decimals: `LegacyDec` and `Dec`. `LegacyDec` is the older decimal type that is still available for use, while `Dec` is the newer, more performant decimal type. The implementation of `Dec` is adapted from Regen Network's `regen-ledger`, specifically from [this module](https://github.com/regen-network/regen-ledger/tree/main/types/math). Migrating from `LegacyDec` to `Dec` involves state-breaking changes, specifically:

* **Data Format**: The internal representation of decimals changes, affecting how data is stored and processed.
* **Precision Handling**: `Dec` supports flexible precision up to 34 decimal places, unlike `LegacyDec` which has a fixed precision of 18 decimal places.

These changes require a state migration to update existing decimal values to the new format. It is recommended to use `Dec` for new modules to leverage its enhanced performance and flexibility.

## Why the Change?

* Historically we have wrapped a `big.Int` to represent decimals in the Cosmos SDK and never had a decimal type. Finally, we have a decimal type that is more efficient and accurate.
* `Dec` uses the [apd](https://github.com/cockroachdb/apd) library for arbitrary precision decimals, suitable for accurate financial calculations.
* `Dec` operations are safer for concurrent use as they do not mutate the original values.
* `Dec` operations are faster and more efficient than `LegacyDec`.

## Using `Dec` in Modules that haven't used `LegacyDec`

If you are creating a new module or updating an existing module that has not used `LegacyDec`, you can directly use `Dec` without any changes.

-- math.NewLegacyDecFromInt64(100)
++ math.NewDecFromInt64(100)

-- math.LegacyNewDecWithPrec(100, 18)
++ math.NewDecWithPrec(100, 18)

-- math.LegacyNewDecFromStr("100")
++ math.NewDecFromStr("100")

-- math.LegacyNewDecFromStr("100.000000000000000000").Quo(math.LegacyNewDecFromInt(2))
++ math.NewDecFromStr("100.000000000000000000").Quo(math.NewDecFromInt(2))

-- math.LegacyNewDecFromStr("100.000000000000000000").Add(math.LegacyNewDecFromInt(2))
++ math.NewDecFromStr("100.000000000000000000").Add(math.NewDecFromInt(2))

-- math.LegacyNewDecFromStr("100.000000000000000000").Sub(math.LegacyNewDecFromInt(2))
++ math.NewDecFromStr("100.000000000000000000").Sub(math.NewDecFromInt(2))


## Modules migrating from `LegacyDec` to `Dec`

When migrating from `LegacyDec` to `Dec`, you need to update your module to use the new decimal type. **These types are state breaking changes and require a migration.**

## Precision Handling

The reason for the state breaking change is the difference in precision handling between the two decimal types:

* **LegacyDec**: Fixed precision of 18 decimal places.
* **Dec**: Flexible precision up to 34 decimal places using the apd library.

## Impact of Precision Change

The increase in precision from 18 to 34 decimal places allows for more detailed decimal values but requires data migration. This change in how data is formatted and stored is a key aspect of why the transition is considered state-breaking.

## Converting `LegacyDec` to `Dec` without storing the data

If you would like to convert a `LegacyDec` to a `Dec` without a state migration changing how the data is handled internally within the application logic and not how it's stored or represented. You can use the following methods.

```go
func LegacyDecToDec(ld LegacyDec) (Dec, error) {
return NewDecFromString(ld.String())
}
```

```go
func DecToLegacyDec(ld Dec) (LegacyDec, error) {
return LegacyDecFromString(ld.String())
}
```

Loading
Loading