views:

192

answers:

5

I am maintaining a Python script that uses xlrd to retrieve values from Excel spreadsheets, and then do various things with them. Some of the cells in the spreadsheet are high-precision numbers, and they must remain as such. When retrieving the values of one of these cells, xlrd gives me a float such as 0.38288746115497402.

However, I need to get this value into a string later on in the code. Doing either str(value) or unicode(value) will return something like "0.382887461155". The requirements say that this is not acceptable; the precision needs to be preserved.

I've tried a couple things so far to no success. The first was using a string formatting thingy:

data = "%.40s" % (value) 
data2 = "%.40r" % (value) 

But both produce the same rounded number, "0.382887461155".

Upon searching around for people with similar problems on SO and elsewhere on the internet, a common suggestion was to use the Decimal class. But I can't change the way the data is given to me (unless somebody knows of a secret way to make xlrd return Decimals). And when I try to do this:

data = Decimal(value)

I get a TypeError: Cannot convert float to Decimal. First convert the float to a string. But obviously I can't convert it to a string, or else I will lose the precision.

So yeah, I'm open to any suggestions -- even really gross/hacky ones if necessary. I'm not terribly experienced with Python (more of a Java/C# guy myself) so feel free to correct me if I've got some kind of fundamental misunderstanding here.

EDIT: Just thought I would add that I am using Python 2.6.4. I don't think there are any formal requirements stopping me from changing versions; it just has to not mess up any of the other code.

+1  A: 

You can use repr() to convert to a string without losing precision, then convert to a Decimal:

>>> from decimal import Decimal
>>> f = 0.38288746115497402
>>> d = Decimal(repr(f))
>>> print d
0.38288746115497402
eldarerathis
doc says that it's only "guaranteed" in 2.7+ (and probably 3.1+)
arthurprs
I tried using repr() explicitly in 2.6.4, and it still rounded. I'm getting version 2.7 right now though to try that out.
jloubert
Hm, I'm actually not clear on what the deal is with `repr()`. It's definitely in the 2.6 docs: http://docs.python.org/release/2.6/library/functions.html?highlight=repr#repr
eldarerathis
Rounds in 3.1 and 2.6 for me. As expected, because this is under `sys.float_info.epsilon` on my system.
katrielalex
Ugh, this is well within my epsilon, so I guess I can't be much more helpful. Anything I test probably won't be portable.
eldarerathis
doing that and later `str(d)` on it works for me. I'm on 2.6.5 according to `--version`.
vlad003
@vlad003: Well, what does `sys.float_info.epsilon` report for you?
eldarerathis
Alright, so repr() appears to do the trick. In Python 2.7 anyway. What is the deal with epsilons though? I mean I know what they are, but are they different from machine to machine? And if so, how can they be changed?
jloubert
@eldarerathis: 2.2204460492503131e-16
vlad003
@jloubert: I *thought* that it was defined in the `<float.h>` header somewhere, and was standardized by the IEEE. I haven't found anything that indicates a different answer yet, but I'm left to wonder what kind of system setup @katrielalex has that causes him to have a larger epsilon. Python just uses the value of `DBL_EPSILON` on your system, AFAIK.
eldarerathis
**decimal is NOT needed. epsilons are irrelevant. xlrd doesn't lose precision. don't hack xlrd code. just use repr(). See my answer**
John Machin
@arthurprs: Where do the docs say that?
Mark Dickinson
@Mark Dickinson: http://docs.python.org/release/2.7/whatsnew/2.7.html
arthurprs
@arthurprs: Hmm. You're right. That sentence ("... repr() of a floating-point number x returns a result that’s guaranteed to round back to the same number when converted back to a string.") *is* rather misleading, since it suggests that this is a new feature when it's not. I'll fix it. Thanks!
Mark Dickinson
A: 

EDIT: Cleared my previous answer b/c it didn't work properly.

I'm on Python 2.6.5 and this works for me:

a = 0.38288746115497402
print repr(a)
type(repr(a))    #Says it's a string

Note: This just converts to a string. You'll need to convert to Decimal yourself later if needed.

vlad003
In Python 2.6.4, string._float() is still rounding. What version are you using?
jloubert
Rewritten my answer. I think that `string._float` is actually still a float. (I don't know why it's in the string module, but that's probably why there's an `_` at the beginning). Sorry for incorrect answer.
vlad003
-1 Missed the point -- the problem is that `float` has limited accuracy, not that its representation is flawed. And `repr( <anything> )` is a string. `Decimal` is definitely needed if you want to store the number in a Python datatype.
katrielalex
I say not: "However, I need to get this value into a string later on in the code." And even using `Decimal` you'd still need to convert to a string. So I say my answer doesn't miss the point. OP needed to convert to a string without the rounding error.
vlad003
+1  A: 

EDIT: I am wrong. I shall leave this answer here so the rest of the thread makes sense, but it's not true. Please see John Machin's answer above. Thanks guys =).

If the above answers work that's great -- it will save you a lot of nasty hacking. However, at least on my system, they won't. You can check this with e.g.

import sys
print( "%.30f" % sys.float_info.epsilon )

That number is the smallest float that your system can distinguish from zero. Anything smaller than that may be randomly added or subtracted from any float when you perform an operation. This means that, at least on my Python setup, the precision is lost inside the guts of xlrd, and there seems to be nothing you can do without modifying it. Which is odd; I'd have expected this case to have occurred before, but apparently not!

It may be possible to modify your local xlrd installation to change the float cast. Open up site-packages\xlrd\sheet.py and go down to line 1099:

...
elif rc == XL_INTEGER:
                    rowx, colx, cell_attr, d = local_unpack('<HH3sH', data)
                    self_put_number_cell(rowx, colx, float(d), self.fixed_BIFF2_xfindex(cell_attr, rowx, colx))
...

Notice the float cast -- you could try changing that to a decimal.Decimal and see what happens.

katrielalex
Thanks for the answer! Obviously I'd prefer to avoid editing the `xlrd` library, as that will undoubtedly get silly; however, I'll definitely give this a shot if nothing else ends up working.
jloubert
@katriealex: Please read my answer.
John Machin
@katriealex: Can you explain exactly what 'doesn't work' on your system? The suggestions to use `repr` should work fine.There's some misinformation here. `sys.float_info.epsilon` is *not* the smallest float that can be distinguished from zero; that value would be around `5e-324` on most machines. (Though IronPython currently does actually have this wrong value for sys.float_info.epsilon, I believe.) And the 'randomly added or subtracted' isn't really helpful either, and is a long way from describing what actually happens.
Mark Dickinson
@Mark: I have explained in a comment on your post (I'm not sure why =)).
katrielalex
+7  A: 

Hello, I'm the author of xlrd. There is so much confusion in other answers and comments to rebut in comments so I'm doing it in an answer.

@katriealex: """precision being lost in the guts of xlrd""" --- entirely unfounded and untrue. xlrd reproduces exactly the 64-bit float that's stored in the XLS file.

@katriealex: """It may be possible to modify your local xlrd installation to change the float cast""" --- I don't know why you would want to do this; you don't lose any precision by floating a 16-bit integer!!! In any case that code is used only when reading Excel 2.X files (which had an INTEGER-type cell record). The OP gives no indication that he is reading such ancient files.

@jloubert: You must be mistaken. "%.40r" % a_float is just a baroque way of getting the same answer as repr(a_float).

@EVERYBODY: You don't need to convert a float to a decimal to preserve the precision. The whole point of the repr() function is that the following is guaranteed:

float(repr(a_float)) == a_float

Python 2.X (X <= 6) repr gives a constant 17 decimal digits of precision, as that is guaranteed to reproduce the original value. Later Pythons (2.7, 3.1) give the minimal number of decimal digits that will reproduce the original value.

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32
>>> f = 0.38288746115497402
>>> repr(f)
'0.38288746115497402'
>>> float(repr(f)) == f
True

Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win32
>>> f = 0.38288746115497402
>>> repr(f)
'0.382887461154974'
>>> float(repr(f)) == f
True

So the bottom line is that if you want a string that preserves all the precision of a float object, use preserved = repr(the_float_object) ... recover the value later by float(preserved). It's that simple. No need for the decimal module.

John Machin
thanks for the explanation :D
arthurprs
@John: thank you very much for the clarification, I have changed my answer to mention yours! However -- I have just rechecked this on my system and it still does not reproduce the functionality that I believe the OP wanted. Specifically, if I define `f = 0.38...97402` and then call `repr(f)`, I get `0.38...974`. Cast to a float, this number compares equal to `f` (they differ by less than my epsilon), but clearly does not maintain the full precision of the original definition. If I do `d = decimal.Decimal( "0.38...97402" )`, full precision is returned with `str(d)`. Have I missed something?
katrielalex
Oh, I think I get it. Is the point you're making that Excel internally uses `double`s, same as Python, so that the apparent higher precision is an illusion?
katrielalex
@katriealex: What Excel uses internally is a bit of a mystery, but in one of Bill Kahan's delicious diatribes (http://www.cs.berkeley.edu/~wkahan/Mindless.pdf) he shows that Excel appears to use "doubles" but stuffs about trying to pretend it's using 15-digit floating decimal arithmetic. However what matters is that number cells in an XLS file contain 64-bit IEEE floats (or a crufty compressed version thereof) and xlrd drags those out with no loss of precision. I thought the non-existent precision difference between the OP's 2.6 repr and your 2.7 repr had already been explained.
John Machin
+1 just for being the author of *xlrd*. Great answer, too :)
kaloyan
@kaloyan: Thanks :)
John Machin
John: yes, repr() is sufficient if you want to just print the precise value. However, I think the comments on converting to Decimal would still be relevant if you want to do additional calculations with the numbers with a higher level of precision. For example, if you read a bunch of rows with float values and then use float arithmetic to sum them you could get rounding errors. Converting those to Decimals before summing would prevent that.
Matt Good
@Matt Good: The OP has not responded to a request to say what he wants to do with the non-float representations of his "high precision numbers". *IF* the problem was loss of precision on summation, then one could use Kahan's (that man again!) accurate summation algorithm. What higher degree of precision? The numbers can't be made any more precise than they are already.
John Machin
Thanks for the clear response, John. I am 95% sure using `repr()` in Python 2.6.4 (both explicitly and via string formatting) still provided a rounded number. But it works as you say in 2.7, and my higher-ups are fine with upgrading versions, so I won't ask any more questions :) Matt Good is correct in that converting to `Decimals` will be necessary for further calculations, but that is an area in the software requirements that is still up in the air. I only need strings for the time being.
jloubert
@jloubert: "95% sure"??? It either does or it doesn't! Don't walk away from a problem! Produce evidence; repeat my Python 2.6.4 example and show what you get. Show (as I did) the Python banner line so that we can see exactly what Python implementation is "producing a rounded answer", if that happens. And a quick silly question: how did you know xlrd was producing `0.38288746115497402` without using `repr()`???
John Machin
Well, I say 95% sure because I was relying on the debugging features of PythonWin, not directly typing lines into an interpreter. This debugger showed that the `floats` produced by `xlrd` were indeed accurate, and that the precision was lost upon using `str()` or `unicode()` or `repr()`. I'll see if I can repeat the results with 2.6.4 when I have some time later today.
jloubert
@jloubert: Still waiting for you to check your assertion. I don't understand why PythonWin debugger (itself an interpreter) is more reliable than typing lines into the official Python interpreter. Accurate when compared with what? Please do take a minute or two to run `python -c"f=0.38288746115497402;print repr(f), float(repr(f))==f"` at the command prompt using Python 2.6.4
John Machin
A: 

As has already been said, a float isn't precise at all - so preserving precision can be somewhat misleading.

Here's a way to get every last bit of information out of a float object:

>>> from decimal import Decimal
>>> str(Decimal.from_float(0.1))
'0.1000000000000000055511151231257827021181583404541015625'

Another way would be like so.

>>> 0.1.hex()
'0x1.999999999999ap-4'

Both strings represent the exact contents of the float. Allmost anything else interprets the float as python thinks it was probably intended (which most of the time is correct).

stefano palazzo