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

Migrate from f32 and f64 to rust_decimal::Decimal #37

Open
2 tasks
pan93412 opened this issue Jul 31, 2022 · 3 comments
Open
2 tasks

Migrate from f32 and f64 to rust_decimal::Decimal #37

pan93412 opened this issue Jul 31, 2022 · 3 comments

Comments

@pan93412
Copy link
Collaborator

pan93412 commented Jul 31, 2022

Abstract

f32 and f64 has some precision issues1, which may be critical for finance calculation2. You can find it with this interactive converter.

crypto-crawler currently converts all the decimal float numbers (String) to binary float numbers (f32/f64), and stores values in Float/f32/f64. It makes the numbers inaccurate and may introduce issues when using this in production (for example, the wrong determination and time point to buy and sell3.)

Solution

To prevent the potential issue, I propose to use rust_decimal::Decimal instead of f32 or f64. It stores float numbers in decimal and thus prevents infinite precision due to the binary representation.

I've tried this library in my project (though has not been in production yet)4, and I can help rewrite this to Decimal.

TBDs (To Be Discussed)

Footnotes

  1. (Chinese) https://medium.com/starbugs/see-why-floating-point-error-can-not-be-avoided-from-ieee-754-809720b32175

  2. (Chinese, ⚠️ Content Farm) https://www.twblogs.net/a/5eec07e211be511c38938805

  3. (Chinese) https://zh.wikipedia.org/zh-tw/捨入誤差#举例

  4. wmjtyd/libstock (1) wmjtyd/libstock (2)

@soulmachine
Copy link
Collaborator

In practice I found that f32 is enough, do you really need that high precision?

@pan93412
Copy link
Collaborator Author

pan93412 commented Aug 2, 2022

In practice I found that f32 is enough, do you really need that high precision?

I think it is still better to prevent such a precision loss even though we don't need such a high precision at this moment, as the retrieved data is a decimal float number instead of a binary one.

@soulmachine
Copy link
Collaborator

The crypto-crawler library doesn't do any parsing at all, while the crypto-msg-parser does. The crypto-crawler library outputs original messages without information loss.

In which case do you need high precision? I don't see additional value of Decimal.

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

No branches or pull requests

2 participants