[Tutor] numerical problem

Kent Johnson kent37 at tds.net
Thu Oct 15 13:03:41 CEST 2009

On Thu, Oct 15, 2009 at 5:28 AM, Mr Timothy Hall
<timothy.hall at uqconnect.edu.au> wrote:

> whenever i run this part of the program, no matter what values i put in for
> vto, k1t etc... it always equals zero.
> im slightly aware of floating point numbers but im not sure why this will
> not give me a real answer.
> when i do it on my calculator the answer is 0.897 for the correct values of
> k3t etc. so its not like the result
> is extremely small.
> any help would be great.
> def runge_tang(vto,k1t,k3t,k4t,k5t,k6t):
>     vn =
> vto+(16/135)*k1t+(6656/12825)*k3t+(28561/56430)*k4t-(9/50)*k5t+(2/55)*k6t
>     return vn

By default, division of integers in Python (and many other languages)
produces another integer, for example:
In [28]: 16/235
Out[28]: 0

So all your constants are 0!

There are two ways around this:
Use floats:
In [29]: 16.0 /235.0
Out[29]: 0.068085106382978725

Use future division:
In [30]: from __future__ import division

In [31]: 16/235
Out[31]: 0.068085106382978725


More information about the Tutor mailing list