Clarification on the Decimal type in Python

后端 未结 4 1707
挽巷
挽巷 2020-12-05 02:01

Everybody know, or at least, every programmers should know, that using the float type could lead to precision errors. However, in some cases, an exact solution

4条回答
  •  臣服心动
    2020-12-05 02:53

    is there a way to have a Decimal type with an infinite precision?

    No; for any non-empty interval on the real line, you cannot represent all the numbers in the set with infinite precision using a finite number of bits. This is why Fraction is useful, as it stores the numerator and denominator as integers, which can be represented precisely:

    >>> Fraction("1.25")
    Fraction(5, 4)
    

提交回复
热议问题