【问题标题】:How to get a list of decimals in python 3如何在python 3中获取小数列表
【发布时间】:2018-04-24 12:07:23
【问题描述】:

不,这不是重复的,上面的链接特别是我所指的不是正确答案。该链接和我在这里的帖子专门询问有关生成十进制列表的问题。但是“答案”会产生一个浮动列表。

正确的答案是使用带有 np.arange 的 Decimal 参数,如 `x_values = np.arange(Decimal(-2.0), Decimal(2.0), Decimal(0.1)) 谢谢https://stackoverflow.com/users/2084384/boargules

我相信这可能会在其他地方得到回答,但我发现的答案似乎是错误的。我想要一个从 -2 到 2 的小数列表(精度 = 1 个小数位)。 -2, -1.9, -1.8 ... 1.8, 1.9, 2.0

当我这样做时:

import numpy as np
x_values = np.arange(-2,2,0.1)
x_values

我明白了:

array([ -2.00000000e+00,  -1.90000000e+00,  -1.80000000e+00, ...

我试过了:

from decimal import getcontext, Decimal

getcontext().prec = 2
x_values = [x for x in np.around(np.arange(-2, 2, .1), 2)]
x_values2 = [Decimal(x) for x in x_values]
x_values2

我明白了:

[Decimal('-2'),
 Decimal('-1.899999999999999911182158029987476766109466552734375'),
 Decimal('-1.8000000000000000444089209850062616169452667236328125'), ...

我在 jupyter notebook 中运行 3.6.3。

更新:我将范围从 2 更改为 2.0。这改善了结果,但我仍然得到一个舍入错误:

import numpy as np
x_values = np.arange(-2.0, 2.0, 0.1)
x_values

产生:

-2.00000000e+00,  -1.90000000e+00,  -1.80000000e+00, ...
 1.00000000e-01,   1.77635684e-15,   1.00000000e-01, ...
 1.80000000e+00,   1.90000000e+00

注意 1.77635684e-15 可能是一个非常小的数字,但它不是零。零测试将失败。因此输出是错误的。

我对重复断言的回应。正如您从我的结果中看到的那样,How to use a decimal range() step value? 的答案不会产生与我在不同范围内看到的结果相同的结果。具体来说,浮点数仍在返回且未四舍五入,并且 1.77635684e-15 不等于零。

【问题讨论】:

  • 你是专门找一个numpy数组吗?
  • 看看numpy.isclose()函数。您可以比较相对和绝对容差。这可以解决您的问题。
  • 如果你想要小数,为什么要从浮点数数组开始?所做的只是将浮点表示固有的不精确性继承为十进制,而不是修复它。这将为您提供所需的列表:[Decimal('-2.0') + Decimal(n) / Decimal(10) for n in range(0,41)].
  • 在给Decimal 提供字符串时,我会得到更好的结果,例如np.arange(Decimal('-2.0'), Decimal('2.0'), Decimal('0.1'))。要了解原因,请查看Decimal(0.1)

标签: python list numpy decimal


【解决方案1】:

围绕一个简单的解决方案进行讨论和重复:

In [177]: np.arange(Decimal('-2.0'), Decimal('2.0'), Decimal('0.1')) 
Out[177]: 
array([Decimal('-2.0'), Decimal('-1.9'), Decimal('-1.8'), Decimal('-1.7'),
       Decimal('-1.6'), Decimal('-1.5'), Decimal('-1.4'), Decimal('-1.3'),
       Decimal('-1.2'), Decimal('-1.1'), Decimal('-1.0'), Decimal('-0.9'),
       Decimal('-0.8'), Decimal('-0.7'), Decimal('-0.6'), Decimal('-0.5'),
       Decimal('-0.4'), Decimal('-0.3'), Decimal('-0.2'), Decimal('-0.1'),
       Decimal('0.0'), Decimal('0.1'), Decimal('0.2'), Decimal('0.3'),
       Decimal('0.4'), Decimal('0.5'), Decimal('0.6'), Decimal('0.7'),
       Decimal('0.8'), Decimal('0.9'), Decimal('1.0'), Decimal('1.1'),
       Decimal('1.2'), Decimal('1.3'), Decimal('1.4'), Decimal('1.5'),
       Decimal('1.6'), Decimal('1.7'), Decimal('1.8'), Decimal('1.9')],
      dtype=object)

将浮点值赋予Decimal 效果不佳:

In [180]: np.arange(Decimal(-2.0), Decimal(2.0), Decimal(0.1)) 
Out[180]: 
array([Decimal('-2'), Decimal('-1.899999999999999994448884877'),
       Decimal('-1.799999999999999988897769754'),
       Decimal('-1.699999999999999983346654631'),

因为Decimal(0.1)只是巩固了0.1的浮点不精确性:

In [178]: Decimal(0.1)
Out[178]: Decimal('0.1000000000000000055511151231257827021181583404541015625')

建议重复:How to use a decimal range() step value?

【讨论】:

    【解决方案2】:

    来自numpy docs -

    import numpy as np
    np.set_printoptions(suppress=True)
    

    将确保“始终使用定点表示法打印浮点数,在这种情况下,当前精度为零的数字将打印为零”

    In[2]: import numpy as np
    
    In[3]: np.array([1/50000000])
    Out[3]: array([2.e-08])
    
    In[4]: np.set_printoptions(suppress=True)
    In[5]: np.array([1/50000000])
    Out[5]: array([0.00000002])
    
    In[6]: np.set_printoptions(precision=6)
    In[7]: np.array([1/50000000])
    Out[7]: array([0.])
    
    In[8]: x_values = np.arange(-2,2,0.1)
    In[9]: x_values
    Out[9]: 
    array([-2. , -1.9, -1.8, -1.7, -1.6, -1.5, -1.4, -1.3, -1.2, -1.1, -1. ,
           -0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1,  0. ,  0.1,
            0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ,  1.1,  1.2,
            1.3,  1.4,  1.5,  1.6,  1.7,  1.8,  1.9])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-09
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 2015-01-13
      • 2010-11-21
      相关资源
      最近更新 更多