简单的乘法和除法非常不准确

Simple multiplication and division being wildly inaccurate

tldr:公式 x / (1 * 1) 减去数百万亿。为什么?

大家好!我是 python 的新手,在从头开始制作 python 消息加密器时,(我知道,有更简单的方法来制作消息编码器。我制作它只是为了体验和乐趣)非常简单的乘法除法问题似乎非常不准确。基本上,我的编码器通过将输入字符串中的所有字符转换为数字,然后将数字乘以两个单独的用户提供的键来工作。我一直在将所有键设置为 1 进行测试,并且编码工作正常;解码是问题。我的公式是:

编码:

String = String * Key1 * Key2     #Both keys are set to 1

解码:

String = String / (Key1 * Key2)     #Again, both keys are set to 1, so in theory it should just spit the string back out.

当我输入 370190160220170180330190140310320 时,我得到 370190160220170177391212613337088.

370190160220170180330190140310320

370190160220170177391212613337088

所以一个等价于 x / (1 * 1) 的公式显然意味着减去多少千万亿。这里发生了什么?就像我说的,我是 python 的新手,所以这个问题的解决方案可能非常简单,但我就是想不出来。

float 除法 (/) 不是任意精确的。 int除法(//)不过!

试试这个:

assert 370190160220170180330190140310320 == 370190160220170180330190140310320 // 1
assert 370190160220170180330190140310320 != 370190160220170180330190140310320 / 1

对于您正在做的事情,您可能希望使用 //% 而不是 /!

您需要使用 Decimal 来准确拟合更大的数字,并且您需要将精度更改为大于默认值 28。

from decimal import Decimal
x = Decimal(370190160220170180330190140310320)
print(x == (x/(1*1))) # False
from decimal import Decimal, getcontext
getcontext().prec = 40
x = Decimal(370190160220170180330190140310320)
print(x == (x/(1*1))) # True