且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python 中的对数不准确

更新时间:2023-12-02 21:56:28

这是计算机算术的预期结果.它遵循特定规则,例如 IEEE 754,这可能与您所学的数学不匹配在学校.

This is to be expected with computer arithmetic. It is following particular rules, such as IEEE 754, that probably don't match the math you learned in school.

如果这实际上很重要,请使用 Python 的十进制类型.

If this actually matters, use Python's decimal type.

示例:

from decimal import Decimal, Context
ctx = Context(prec=20)
two = Decimal(2)
ctx.divide(ctx.power(two, Decimal(31)).ln(ctx), two.ln(ctx))