python log() function

Oleg Broytmann phd at phd.pp.ru
Sat Dec 8 07:37:23 EST 2001


On Sat, Dec 08, 2001 at 12:16:15PM -0000, Ian wrote:
> Hello I am new to python and am trying to use it to work out a sum
> log(20/7) / log(2)
> which gives result
> 1.0
> 
> I am expecting to get 1.51 as a result, and using different langs such as
> perl or php, I get the desired result of 1.51
> 
> It seems to me that python only goes to a certain decimal place, not as much
> as the other compilers, can this be altered in anyway?

phd at phd 1 >> python
Python 2.1.1 (#1, Aug  8 2001, 19:17:29)
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> 20/7
2

   This is integer division. So you've calculted log(2)/log(2) :) Use
floting point division: 20.0/7.

phd at phd 3 >> python
Python 2.1.1 (#1, Aug  8 2001, 19:17:29)
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> from math import log
>>> log(20.0/7)/log(2)
1.5145731728297582

Oleg.
-- 
     Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.




More information about the Python-list mailing list