[SciPy-user] Representation of floating-point numbers

Arnd Baecker arnd.baecker at web.de
Thu Jan 15 12:31:13 CST 2004


Hi Nils,

On Thu, 15 Jan 2004, Nils Wagner wrote:

> Hi all,
>
> Python 2.3+ (#1, Sep 23 2003, 23:07:16)
> [GCC 3.3.1 (SuSE Linux)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> 5.1
> 5.0999999999999996
> >>>
>
> What is the reason for this strange behavior ?

Have look at the Python Tutorial,
  http://www.python.org/doc/current/tut/node15.html

> octave:1> format long
> octave:2> 5.1
> ans = 5.10000000000000
>
> Please can someone expand on the different behavior of python and
> octave.

Notice that the number of digits is different,
see below for some examples
There might be a way to change the representation in octave.

Arnd

P.S.: I hope the difference between 5.0999999999999996
      and 5.1 does not matter for your numerics ;-)


In [1]: print 5.1
5.1

In [2]: 5.1
Out[2]: 5.0999999999999996

In [3]: print "%10.9f", 5.1
%10.9f 5.1

In [4]: print "%10.9f" %( 5.1)
5.100000000

In [5]: print "%f" %( 5.1)
5.100000

In [6]: print "%g" %( 5.1)
5.1

In [7]: print "%16.15f" %( 5.1)
5.100000000000000

In [8]: print "%18.16f" %( 5.1)
5.0999999999999996










More information about the SciPy-user mailing list