Closed skirpichev closed 9 months ago
What's the advantage of this approach over the one illustrated in the following example: https://github.com/aroberge/ideas/blob/master/ideas/examples/decimal_math.py?
Sorry, my bad - I miss that example. This indeed looks simpler at the tokenization phase.
On another hand, this heuristics not handle all valid float literals, e.g. 1e-10
.
Python supports floating point arithmetic of fixed precision (usually something like 53 bits in mantissa). So, "big enough" floating point literals silently truncated:
But we could recover all digits!
Given this, we could transform arbitrary floating point literals to Decimal instances and then do arithmetic. Similar AST transformation could be useful for arbitrary-precision floating point arithmetic libraries in Python (e.g. mpmath) or CAS (e.g. diofant).
Edit: see https://github.com/diofant/diofant/pull/1378/commits/27ecabc25facf624dbdd4cf18efcb1b85f854dae as an implementation example.