English 中文(简体)
浮标
原标题:Python float copy question

I m puzzled by some behaviour I m seeing when copying a float array member into another variable - please help!

例如

data_entry[1] = 9.6850069951

new_value = data_entry[1]


<comment> #print both

9.6850069951


9.6850663300

I m aware of the problem of binary storage of floats but I thought with a direct copy of memory we would end up with the same value.

Any ideas? I need better precision than this! thanks in advance Stuart

问题回答

在转让后,变数的新价值不是浮标的复印件,它只是另外提到同一物体。 因此,不可能有不同的印本。 因此,在原始问题中无疑略去了一些细节。

斯图尔特——请你尝试以下工作并公布结果,或告诉我们你的实际法典如何变化。 请注意,新的“数值数据_enter? 即两者均为同一目标。

>> data_entry = [0,0]
>> data_entry[1] = 9.6850069951
>> new_value = data_entry[1]
>> new_value is data_entry[1]
True
>> print data_entry[1], new_value
9.6850069951 9.6850069951

如果您真正使用array模块(或numpy s ranges),那么精确损失很容易解释,例如:

>>> dataentry = array.array( f , [9.6850069951])
>>> dataentry[0]
9.6850070953369141

www.un.org/Depts/DGACM/index_french.htm 说我们再使用32个轨道浮标,因此只有大约7个重要数字“保险”。 但是,很容易使用64轨浮动(在所谓的“双重精确”时):

>>> dataentry = array.array( d , [9.6850069951])
>>> dataentry[0]
9.6850069951000002

如你所看到,这样,十多位“保险”的重要数字(通常可以依靠约14+,除非你做算术的“oo”,如在人数上作非常接近的区别,这当然会损害你的准确性;-)

我的双线工作与关于含水层的2.6.2:

>>> data_entry = [1, 2]
>>> data_entry[1] = 9.6850069951
>>> new_value = data_entry[1]
>>> print data_entry[1]
--> print(data_entry[1])
9.6850069951
>>> print new_value
--> print(new_value)
9.6850069951

一种选择是转向使用 Dec物体:

>>> from decimal import Decimal
>>> data_entry[1] = Decimal( 9.6850069951 )
>>> new_value = data_entry[1]
>>> print data_entry[1]
--> print(data_entry[1])
9.6850069951
>>> print new_value
--> print(new_value)
9.6850069951

如果你重新失去精确性,这可能会有所帮助。

你们把一些法典排除在外。

>>> data_entry=[0,0]
>>> data_entry[1] = 9.6850069951
>>> 
>>> new_value = data_entry[1]
>>> print data_entry
[0, 9.6850069951000002]
>>> print new_value
9.6850069951
>>> print data_entry[1]
9.6850069951

该浮点号的<代码>repr和str正在产生不同的结果。 我的猜测是,你颁布的法典没有提到这一区别。

此处为经过编辑的编码格式:

old code:
data = []
for data_entry in data:
    if (data_entry[1] != 0):
    value = data_entry[1]
    modlog(logging.INFO, raw value = %.12f ,data_entry[1])
    modlog(logging.INFO, value_in = %.12f , value)
output:
:INFO:raw value = 2.334650748292
:INFO:value_in  = 2.334685585881

new code:
data = array.array( d ) 
if (data[index] != 0):
    test_data = data[index]
    modlog(logging.INFO, raw data = %.12f , data[(index)])
    modlog(logging.INFO, test_data = %.12f , test_data)
output:
:INFO:raw data = 2.333840588874
:INFO:test_data= 2.333840588874




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签