![](https://secure.gravatar.com/avatar/b6c3a8039062726b0600fc5de0dafee7.jpg?s=120&d=mm&r=g)
I'm not sure this is a problem, but I'm looking for a solution for this and I wonder if one could give a piece of advice: I have a C extension using doubles and floats. I return a float casted to double to Python, from my extension, and when I display it I have some extra numbers at the end of the "correct" number. In the extension, dgv is a float (in this exemple dgv=0.1). PyTuple_SET_ITEM(tp0, i, PyFloat_FromDouble((double)dgv)); I print it in Python: print tuple[0] Which produces: 0.10000000149 I get to much numbers, because the print should not try to get more then the 4 bytes float. It looks that the floatobject.c files is setting a number precision for printing, which is forced to 12. (#define PREC_STR 12) This work if you use a "double", but not a "double" casted from a "float". This problem occurs either on SGI and DEC. With stdio: printf("%.g\n", (float) dgv); printf("%.g\n", (double)dgv); printf("%.12g\n",(float) dgv); printf("%.12g\n",(double)dgv); produces (this is a "CORRECT" behavior for printf, we're printing too much digits): 0.1 0.1 0.10000000149 0.10000000149 Any idea ? How can I say to Python to forget the precision, or set it as global. Marcvs [alias Yes I could only compute with integers, but... ]
![](https://secure.gravatar.com/avatar/5dde29b54a3f1b76b2541d0a4a9b232c.jpg?s=120&d=mm&r=g)
Marc Poinot wrote:
I'm not sure this is a problem,
It's not.
and when I display it I have some extra numbers at the end of the "correct" number.
What you are seeing is the best decimal representation of the binary number that is stored in that double. While the extra bits in binary of the double over the float should be zero, that does not mean that the extra decimal digits will be zero also. In this case, you are trying to store the value of 1.0 / 10.0, That value can not be represented exactly in binary. The value: 0.10000000149 is as close as you can get with a C float. so you are getting the right answer (subject to the limitations of floating point representation and arithmetic), as demonstrated by your example:
printf("%.12g\n",(float) dgv);
0.10000000149
produces (this is a "CORRECT" behavior for printf, we're printing too much digits)
It depends what you mean by too many. The above example shows what the best decimal value you can get with 12 digits from your float value, which is the same as what Python has in it's double. By the way, your four printf examples also demonstrate that you are getting exactly the same results by casting a float to a double within C, as when you do it passing to Python (which you should expect. A Python Float is a C double, after all) By default, in a print statement, Python displays all the digits that are required to reproduce the number. If you don't want to see all those digits, do what you did in C:
By the way, see: http://www.python.org/doc/current/tut/node14.html For more explaination. -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
![](https://secure.gravatar.com/avatar/5dde29b54a3f1b76b2541d0a4a9b232c.jpg?s=120&d=mm&r=g)
Marc Poinot wrote:
I'm not sure this is a problem,
It's not.
and when I display it I have some extra numbers at the end of the "correct" number.
What you are seeing is the best decimal representation of the binary number that is stored in that double. While the extra bits in binary of the double over the float should be zero, that does not mean that the extra decimal digits will be zero also. In this case, you are trying to store the value of 1.0 / 10.0, That value can not be represented exactly in binary. The value: 0.10000000149 is as close as you can get with a C float. so you are getting the right answer (subject to the limitations of floating point representation and arithmetic), as demonstrated by your example:
printf("%.12g\n",(float) dgv);
0.10000000149
produces (this is a "CORRECT" behavior for printf, we're printing too much digits)
It depends what you mean by too many. The above example shows what the best decimal value you can get with 12 digits from your float value, which is the same as what Python has in it's double. By the way, your four printf examples also demonstrate that you are getting exactly the same results by casting a float to a double within C, as when you do it passing to Python (which you should expect. A Python Float is a C double, after all) By default, in a print statement, Python displays all the digits that are required to reproduce the number. If you don't want to see all those digits, do what you did in C:
By the way, see: http://www.python.org/doc/current/tut/node14.html For more explaination. -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (2)
-
Chris Barker
-
Marc Poinot