Clarification on the Decimal type in Python

后端 未结 4 1672
挽巷
挽巷 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:56

    So, my question is: is there a way to have a Decimal type with an infinite precision?

    No, since storing an irrational number would require infinite memory.

    Where Decimal is useful is representing things like monetary amounts, where the values need to be exact and the precision is known a priori.

    From the question, it is not entirely clear that Decimal is more appropriate for your use case than float.

提交回复
热议问题