Representation of floating-point numbers
![](https://secure.gravatar.com/avatar/d41fa6e1fe29e6c5c5821b5a3f31f190.jpg?s=120&d=mm&r=g)
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 ? GNU Octave, version 2.1.49 (i686-pc-linux-gnu). Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 John W. Eaton. This is free software; see the source code for copying conditions. There is ABSOLUTELY NO WARRANTY; not even for MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, type `warranty'. Please contribute if you find this software useful. For more information, visit http://www.octave.org/help-wanted.html Report bugs to <bug-octave@bevo.che.wisc.edu>. octave:1> format long octave:2> 5.1 ans = 5.10000000000000 Please can someone expand on the different behavior of python and octave. Nils
![](https://secure.gravatar.com/avatar/beebe07772844149dcd47e23e5276e72.jpg?s=120&d=mm&r=g)
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
participants (2)
-
Arnd Baecker
-
Nils Wagner