You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
It introduces breaking changes. A safer non-breaking way is to write a feature flag and manually enable Decimal; however, it complexes the codebase.
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.
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.
Abstract
f32
andf64
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 inFloat/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 off32
orf64
. 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)
Decimal
; however, it complexes the codebase.Decimal
or convert aDecimal
to a number. Note thatDecimal::to_f64(self)
returns aOption<f64>
, and theOption
may beNone
iff64
is not enough to represent thisDecimal
.Footnotes
(Chinese) https://medium.com/starbugs/see-why-floating-point-error-can-not-be-avoided-from-ieee-754-809720b32175 ↩
(Chinese,⚠️ Content Farm) https://www.twblogs.net/a/5eec07e211be511c38938805 ↩
(Chinese) https://zh.wikipedia.org/zh-tw/捨入誤差#举例 ↩
wmjtyd/libstock (1) wmjtyd/libstock (2) ↩
The text was updated successfully, but these errors were encountered: