开发者

floats inside tuples changing values when accessed

So I have a list of tuples of two floats each. Each tuple represents a range. I am going through another list of floats which represent values to be fit into the ranges. All of these floats are < 1 but positive, so precision matter. One of my tests to determine if a value fits into a range is failing when it should pass. If I print the value and the range that is causing problems I can tell this much:

curValue = 0.00145000000671 
range = (0.0014500000067055225, 0.0020968749796738849)

The conditional that is failing is:

if curValue > range[0] and ... blah :
    # do some stuff

From the values given by curValue and range, the test should clearly pass (don't worry about what is in the conditional). Now, if I print explicitly what the value of range[0] is I get:

 range[0] =  0.00145000000671

Which would explain why the test is failing. So my question then, is why is the float changing when it is accessed. It has decimal values available up to a certain precision when part of a tuple, and a different precision when accessed. 开发者_运维问答Why would this be? What can I do to ensure my data maintains a consistent amount of precision across my calculations?


The float doesn't change. The built-in numberic types are all immutable. The cause for what you're observing is that:

  1. print range[0] uses str on the float, which (up until very recent versions of Python) printed less digits of a float.
  2. Printing a tuple (be it with repr or str) uses repr on the individual items, which gives a much more accurate representation (again, this isn't true anymore in recent releases which use a better algorithm for both).

As for why the condition doesn't work out the way you expect, it's propably the usual culprit, the limited precision of floats. Try print repr(curVal), repr(range[0]) to see if what Python decided was the closest representation of your float literal possible.


In modern day PC's floats aren't that precise. So even if you enter pi as a constant to 100 decimals, it's only getting a few of them accurate. The same is happening to you. This is because in 32-bit floats you only get 24 bits of mantissa, which limits your precision (and in unexpected ways because it's in base2).

Please note, 0.00145000000671 isn't the exact value as stored by Python. Python only diplays a few decimals of the complete stored float if you use print. If you want to see exactly how python stores the float use repr.

If you want better precision use the decimal module.


It isn't changing per se. Python is doing its best to store the data as a float, but that number is too precise for float, so Python modifies it before it is even accessed (in the very process of storing it). Funny how something so small is such a big pain.

You need to use a arbitrary fixed point module like Simple Python Fixed Point or the decimal module.


Not sure it would work in this case, because I don't know if Python's limiting in the output or in the storage itself, but you could try doing:

if curValue - range[0] > 0 and...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜