我正在对Python进行定量分析,并与另一位同事比较结果,我们发现由于操作顺序不同,这些典型的浮点值在小数点后17位左右存在差异。
为了寻找解决方案,我找到了十进制库,阅读了文档并运行了一些示例
# This is using fixed point arithmetic, resulting on what we would expect
Decimal('0.1') + Decimal('0.2') == Decimal('0.3')
# This is using floating point arithmetic, and thus will never reach
# the exact number using base 2, the famous 0.30000000000000004
x = 0.1 + 0.2
# But what is this doing? It outputs 0.3000000000000000166533453694, which
# suggests a floating point usage, but different error
x = Decimal(0.1) + Decimal(0.2)
所以在这一点上,我真的很困惑,为什么这个错误与普通的python float不同,如果我想将这个库与Pandas一起使用,我是否必须将每个操作数强制转换为str?