Open pan93412 opened 1 year ago
In practice I found that f32
is enough, do you really need that high precision?
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 issues[^1], which may be critical for finance calculation[^2]. 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 sell[^3].)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
.[^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/捨入誤差#举例