-
Notifications
You must be signed in to change notification settings - Fork 103
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
Consider a right decimals approach for encoding decimal numbers #149
Comments
I would just note that I'm not sure binary representation is a problem. The SDK has decided to use strings instead. We should discuss there. If we do use binary, I suggest |
However |
In In
|
Which is a proto wrapper around a string not binary. Pretty unnecessary imho to wrap it. There are reasons we use strings in the SDK. Most notably because it's a standardized representation. |
How and why would we use bigint serialization for decimals? Do you mean mantissa and exponent. Wouldn't a varint be better than big median anyway? |
Mantissa only, because the exponent is
Deserializing is simple:
The Advantage of this is that we can push this feature to the upstream (apd). |
Seems like varint will save a few bytes right? |
I don't think so. Because we need to encode the |
If we will like to optimize a storage, we just encode |
and |
Okay well these are details we can optimize later. I first would want to see if we can 1) get buy-in on the SDK side to adopt GDA (cosmos/cosmos-sdk#7773) and 2) whether they're open to a binary format. I personally don't believe we should just invent our own decimal standard which is what was done with |
IMHO even int16 should be plenty for exponent no? |
To be honest, I believe that 16 decimal places (int8) is more than enough. This is only for storing. For intermediate operations we could have more precision. |
|
@clevinson can we close this as I think it's meaningfully tracked elsewhere? |
Summary
For blockchain use-case we need to do arithmetic operation which is both deterministic and support big numbers. Moreover, since numbers are used very often we should have an efficient (storage and cpu wise) serialization.
Context
In Cosmos-SDK we are using
sdk.Int
which is a wrapper around Gobig.Int
. Big integers are pretty efficient for arithmetic operations. Moreoversdk.Int
supports efficient serialization using Int.Bytes()The problem with
sdk.Int
is that it's not very friendly to encode Decimal numbers. In the real world we like to think with divisible units, eg:1.2 USD
rather than120 US Cents
Related Work
We started with an experiment in
x/ecocredits
to use apd. Currently it's used only in that module.Considerations
With respect to cosmos/cosmos-sdk#7113, let's consider and be consistent how we want to represent decimal values across Regen Ledger and SDK.
sdk.Int
and establish a standard in the SDK community about Decimal numbers representation.Goal: we should keep in mind, that we don't want to end up with 15 standards of coin / credit / token amount representation.
References:
sdk.Dec
doesn't work (has bugs).The text was updated successfully, but these errors were encountered: