3 Replies - 469 Views - Last Post: 01 July 2013 - 01:42 PM Rate Topic: -----

#1 chemcoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-May 13

Why does python keep rounding my floats

Posted 30 June 2013 - 05:20 PM

I want this program to do a series of calculations and output the computed value, however, python seems to be round down my floats. Is there away that I can keep the decimal values. My code looks like this


def calc1(conc, fwd, rev, kmfwd, kmrev):
	if conc >= kmfwd:
		frate = fwd
	else:
		ratio1 = conc / kmfwd
		frate = ratio1 *fwd
	
	if conc >= kmrev:
		rrate = rev
	else:
		ratio2 = conc / kmrev
		rrate = ratio2 * rev
	
	final = conc + frate - rrate
	
	print ratio1 #outputs 0 instead 0.5
	print ratio2 #outputs 0 instead 0.5
	print frate #outputs 0 instead 12.5
	print rrate #outputs 0 instead 5
	return final
	
value = calc1(100, 25, 10, 200, 200)

print value #outputs 100 instead of 107.5




Is This A Good Question/Topic? 0
  • +

Replies To: Why does python keep rounding my floats

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2881
  • View blog
  • Posts: 9,555
  • Joined: 12-December 12

Re: Why does python keep rounding my floats

Posted 30 June 2013 - 06:21 PM

value = calc1(100, 25, 10, 200, 200)

These numbers are integers, not floats. By default, Python 2 will perform integer division with expressions like 100 / 200. This behaviour is changed in Python 3.

I would do the following to pass floats to your function:

value = calc1(100.0, 25.0, 10.0, 200.0, 200.0)

(I can't verify this at the moment though, as I have Python 3 running)

Alternatively, for Python 2.2+, add the following to the top of your module:

from __future__ import division

which tells Python 2 to behave in the Python 3 way.

Another option is to specifically cast to floats using float(x):

value = calc1(float(100), float(25), float(10), float(200), float(200))

but I would probably just use 100.0, etc., as mentioned above.

BTW I would get in the habit of using brackets with print: print("whatever"). In Python 3 print is a function, rather than a statement, and brackets are required, but they are optional in Python 2.x.

This post has been edited by andrewsw: 30 June 2013 - 06:27 PM

Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,105
  • Joined: 19-March 11

Re: Why does python keep rounding my floats

Posted 30 June 2013 - 07:40 PM

The float function will return a value as a float.


If you have a list of ints and you don't want to manipulate their representations in the source code (or you're reading them from a text file) you can always use a comprehension to correct representation:

list_of_ints = [1,2,3,4]
list_of_floats = [float (i) for i in list_of_ints]

This post has been edited by jon.kiparsky: 30 June 2013 - 07:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 chemcoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-May 13

Re: Why does python keep rounding my floats

Posted 01 July 2013 - 01:42 PM

Thanks that solved it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1