【发布时间】:2019-08-09 15:27:56
【问题描述】:
另一个问题Python modulo on floats 描述了为什么 Python 的模数在浮点数方面存在问题(我也知道计算机数字不是 essay 中所涵盖的数字,尤其是 3 的倍数)。但是,我在模数方面遇到了类似但不同的问题,并且接受的答案不起作用。
这是一个在 MacBook Pro 上使用 Python 3.7.1 可重现的示例:
import numpy as np
x = np.array([2.012, 2.012 *2, 2.012 *3, 2.012 *4, 2.012 *5, 2.012 *6])
x % 2.012
# array([0.0000000e+00, 0.0000000e+00, 2.0120000e+00, 0.0000000e+00,
# 4.4408921e-16, 2.0120000e+00])
如果我尝试接受的答案,我仍然会遇到同样的问题:
from decimal import Decimal
Decimal(3*2.012) % Decimal(2.012)
# Decimal('2.011999999999999566568931186')
我的具体问题:如何计算模数并为我的示例等系列取零?
根据反馈编辑:
作为一个具体的例子,如果我的数据看起来像这样(具体值将从实际数据中读取),我该如何做模运算符:
## X is read in from real data
x = np.array([2.012, 4.024, 6.036, 8.048, 1, 2, 3, 1.2, 1.3, 1.4, 1, 5, 6, 2.012, 2.012, 2.012])
[D(x) % D(frequency) for x in detections]
#[Decimal('0E-51'), Decimal('0E-51'), Decimal('2.011999999999999566568931186'), Decimal('0E-51'), Decimal('1.000000000000000000000000000'), Decimal('2.000000000000000000000000000'), Decimal('0.9879999999999999893418589636'), Decimal('1.199999999999999955591079015'), Decimal('1.300000000000000044408920985'), Decimal('1.399999999999999911182158030'), Decimal('1.000000000000000000000000000'), Decimal('0.9759999999999999786837179272'), Decimal('1.975999999999999978683717927'), Decimal('0E-51'), Decimal('0E-51'), Decimal('0E-51')]
【问题讨论】:
-
初始值是作为浮点数提供还是作为实际数据中的文本提供?您需要从一开始就考虑将值转换为小数。
-
它们将被 Pandas 或 numpy 读入。我还没有得到那部分代码。
-
我认为它们中的任何一个都没有小数作为内置数据类型。在这种情况下,您可能需要考虑(1)使用整数表示,例如通过将值乘以 1000;或 (2) 保持浮点数表示并对模结果进行四舍五入。
标签: python numpy floating-point modulo