我的代码如下:
pows2nag = np.asarray([ np.power(2.0,x) for x in range(-65,0) ][::-1])
它将生成如下结果:
[ 5.00000000e-01 2.50000000e-01 1.25000000e-01 6.25000000e-02
3.12500000e-02 1.56250000e-02 7.81250000e-03 3.90625000e-03
1.95312500e-03 9.76562500e-04 4.88281250e-04 2.44140625e-04
1.22070312e-04 6.10351562e-05 3.05175781e-05 1.52587891e-05
7.62939453e-06 3.81469727e-06 1.90734863e-06 9.53674316e-07
4.76837158e-07 2.38418579e-07 1.19209290e-07 5.96046448e-08
2.98023224e-08 1.49011612e-08 7.45058060e-09 3.72529030e-09
1.86264515e-09 9.31322575e-10 4.65661287e-10 2.32830644e-10
1.16415322e-10 5.82076609e-11 2.91038305e-11 1.45519152e-11
7.27595761e-12 3.63797881e-12 1.81898940e-12 9.09494702e-13
4.54747351e-13 2.27373675e-13 1.13686838e-13 5.68434189e-14
2.84217094e-14 1.42108547e-14 7.10542736e-15 3.55271368e-15
1.77635684e-15 8.88178420e-16 4.44089210e-16 2.22044605e-16
1.11022302e-16 5.55111512e-17 2.77555756e-17 1.38777878e-17
6.93889390e-18 3.46944695e-18 1.73472348e-18 8.67361738e-19
4.33680869e-19 2.16840434e-19 1.08420217e-19 5.42101086e-20
2.71050543e-20]
我的问题是小数精度并不像我想的那样精确。
例如,2.44140625
除以2应该等于1.220703125
,但我在这里得到的是1.22070312
。
如何让numpy.power生成更长的小数?
答案 0 :(得分:4)
使用np.set_printoptions调整打印数组的精度。
>>> np.set_printoptions(precision=15)
>>> print(pows2nag)
[ 5.000000000000000e-01 2.500000000000000e-01 1.250000000000000e-01
6.250000000000000e-02 3.125000000000000e-02 1.562500000000000e-02
7.812500000000000e-03 3.906250000000000e-03 1.953125000000000e-03
9.765625000000000e-04 4.882812500000000e-04 2.441406250000000e-04
1.220703125000000e-04 6.103515625000000e-05 3.051757812500000e-05
1.525878906250000e-05 7.629394531250000e-06 3.814697265625000e-06
1.907348632812500e-06 9.536743164062500e-07 4.768371582031250e-07
2.384185791015625e-07 1.192092895507812e-07 5.960464477539062e-08
2.980232238769531e-08 1.490116119384766e-08 7.450580596923828e-09
3.725290298461914e-09 1.862645149230957e-09 9.313225746154785e-10
4.656612873077393e-10 2.328306436538696e-10 1.164153218269348e-10
5.820766091346741e-11 2.910383045673370e-11 1.455191522836685e-11
7.275957614183426e-12 3.637978807091713e-12 1.818989403545856e-12
9.094947017729282e-13 4.547473508864641e-13 2.273736754432321e-13
1.136868377216160e-13 5.684341886080801e-14 2.842170943040401e-14
1.421085471520200e-14 7.105427357601002e-15 3.552713678800501e-15
1.776356839400250e-15 8.881784197001252e-16 4.440892098500626e-16
2.220446049250313e-16 1.110223024625157e-16 5.551115123125783e-17
2.775557561562891e-17 1.387778780781446e-17 6.938893903907228e-18
3.469446951953614e-18 1.734723475976807e-18 8.673617379884035e-19
4.336808689942018e-19 2.168404344971009e-19 1.084202172485504e-19
5.421010862427522e-20 2.710505431213761e-20]
答案 1 :(得分:0)
2.44140625/2
确实提供1.220703125
。控制台中显示的实际值和值之间存在差异。看看这个:
np.pi
Out[992]: 3.141592653589793
np.array([np.pi])
Out[993]: array([ 3.14159265])
np.array([np.pi])[0]
Out[994]: 3.1415926535897931