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: add HiLo conversion method #33

Merged
merged 4 commits into from
Dec 30, 2024

Conversation

jokruger
Copy link
Contributor

Decimal type has FromHiLo constructor. The opposite method is required so the decimal type can be constructed / deconstructed. Also, ability to convert to {Hi, Lo} pair is important for integrations with other libraries which work with uint64 or uint128 types.

@quagmt
Copy link
Owner

quagmt commented Dec 29, 2024

ability to convert to {Hi, Lo} pair is important for integrations with other libraries which work with uint64 or uint128 types

Can you elaborate why you need to convert decimal to another data type? I think libraries that has uint128 is not that common

@jokruger
Copy link
Contributor Author

jokruger commented Dec 29, 2024

ability to convert to {Hi, Lo} pair is important for integrations with other libraries which work with uint64 or uint128 types

Can you elaborate why you need to convert decimal to another data type? I think libraries that has uint128 is not that common

Example - https://github.com/tigerbeetle/tigerbeetle
It works with uint128. It expects amounts will be represented as 128 bit integer, scaled to required precision. Because it is double entry accounting, internally it requires only Add function for amounts, and only positive values with same scale. I.e. you can do complicated calculations in type like udecimal, and store it as scaled int128.

Currently I am using shopspring/decimal for this usecase. However, it's performance is not good, and conversion to uint128 through big.Int makes no sense as I am expecting values fit to uint128 in any case.

Another example - any external storage where you also want to be able to apply a simple functions like comparison, add, etc. I.e. scaled int is ideal. But int64 is too small. And udecimal in any case targets int128, so why not allow to export to this type?

Another reason is existing FromHiLo constructor. You can build udecimal from Hi/Lo pair... but you cannot deconstruct it back to Hi/Lo.

@quagmt
Copy link
Owner

quagmt commented Dec 29, 2024

FromHiLo is mainly used for Fuzz and tbh I don't expect it to be utilized. Your use case is valid tho. However, I think the name should be ToHiLo. Also, can you add some examples to doc_test.go to keep the docs up-to-date?

@jokruger
Copy link
Contributor Author

FromHiLo is mainly used for Fuzz and tbh I don't expect it to be utilized. Your use case is valid tho. However, I think the name should be ToHiLo. Also, can you add some examples to doc_test.go to keep the docs up-to-date?

done

Copy link

codecov bot commented Dec 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.14%. Comparing base (2e1d37c) to head (b7acbf0).
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #33      +/-   ##
==========================================
+ Coverage   96.13%   96.14%   +0.01%     
==========================================
  Files           5        5              
  Lines        1863     1868       +5     
==========================================
+ Hits         1791     1796       +5     
  Misses         48       48              
  Partials       24       24              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@quagmt quagmt self-requested a review December 30, 2024 00:43
@quagmt quagmt merged commit 1082a57 into quagmt:master Dec 30, 2024
9 checks passed
@quagmt quagmt changed the title Add HiLo conversion method feat: add HiLo conversion method Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants