Decimal vs float #28
Unanswered
gister9000
asked this question in
Q&A
Replies: 2 comments 7 replies
-
I didn't explore this much, but I'd bet that round can sometimes fail too. This should probably be a second discussion, let's focus on the initial message here. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Have you tried changing the source code from Decimal to float, and did you perform benchmark tests to see how much it improved performance? |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Float is much faster, but less precise. Depending on math operations, Decimal is between 2 and 200 times slower. There are many benchmarks on the Internet, and I've confirmed it myself.
However, float is very imprecise as demonstrated by (Python 3.12.3):
So, if one's use case requires removing or adding price level which is the result of float math operations, decimal is definitely the way to go.
However, usually, people only want local book representation which means all numbers in the book were received as a string from the exchange which means all numbers in the book are the result of
float('string')
which will be mapped to the same value each time:So my conclusion is that if we don't rely on operations like this one, float is a better choice:
Am I missing something?
Beta Was this translation helpful? Give feedback.
All reactions