[Numpy-discussion] Displaying floats with Python

Chris Barker Chris.Barker at noaa.gov
Wed Nov 27 11:19:02 CST 2002


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:

>>> d = 0.10000000149
>>> print d
0.10000000149
>>> print "%g"%d
0.1
>>> print "%.12g"%d
0.10000000149

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 at noaa.gov




More information about the Numpy-discussion mailing list