【问题标题】:Python's Decimal type not Real? [duplicate]Python 十进制类型不是实数? [复制]
【发布时间】:2018-09-19 13:40:01
【问题描述】:

考虑以下几点:

from decimal import Decimal
from numbers import Number, Complex, Real

z = Decimal(0)

# Expected: a == b == c == True
# Actual (Python 3.6.5):
a = isinstance(z, Number)  # True
b = isinstance(z, Complex) # False
c = isinstance(z, Real)    # False

Decimal 似乎实现了根据PEP3141 需要考虑Real 的所有内容,那么给出了什么?值得注意的是,fractions.Fraction 被上述 sn-p 正确分类(例如a == b == c == True)。

【问题讨论】:

    标签: python


    【解决方案1】:

    PEP 3141中其实有提到:

    在与其作者协商后,决定此时不应将 Decimal 类型作为数字塔的一部分。

    所以Decimal 被故意排除在外。

    【讨论】:

    • 教我读到底!
    • @Jason 在寻找原因时,我在这里发现了另一个更详细的问题,因此您也应该检查一下。
    • 感谢您发现这一点——发布的解决方案既显而易见,也正是我所需要的,而且解释也非常翔实,可能使我免于将来出现看似随机的错误。
    猜你喜欢
    • 2021-11-11
    • 2018-06-24
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 2013-06-02
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多