且构网

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

Python中不准确的对数

更新时间:2023-12-02 21:26:10

这对于计算机算术来说是意料之中的.它遵循特定规则,例如 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))