[Tutor] [Long!] Varying degrees of precision

kromag@nsacom.net kromag@nsacom.net
Tue, 31 Jul 2001 16:53:14 -0700 (PDT)

Hi all!

Sorry I've been a stranger lately, but I have been decending into C.

I have noticed that there are some weirdnesses occouring when converting 
integers to floats in python. I wrote the following module as the beginnings 
of a "hot rod math" project. The first function, suck() gives the proper cfm 
flow for a carburetor on an engine of a given size at a given rpm.

------------begin rodney.py-----------
'''Suck gives a good idea of the required carburetor flow for an engine
of a given volume in cubic centimeters at a given RPM. Note:  suck 
does not round up to nearest cfm, but merely strip everything to the 
right of the decimal. If you are really, really anal, replace 'int(cfm) 
with 'cfm' in the lines below and revel in your increased accuracy... 
then cry as you realize you can't buy a carb for your Buick that flows 
at the exactly optimal 347.83669982303041 CF/M. Now calm down.'''

'''usage: suck(350,5500,'sae')
or:      suck(2000.00,6000.00,'cc') <---here it is!
Hint: 'sae' is the only thing you have to remember. 'cc' could be replaced 
by 'booger' and it would still work.'''

def suck(cc,rpm,increment):
	if increment == 'sae':
		cid=cc/16.387 # This line converts from cc to cid- lazy.
	cfm=( cid/2 )*( rpm/1728 )*.85 #Assumes 85% volumetric effeciency.
	#return int( cfm )
	return cfm
------------end rodney.py---------------

rodney.py returns:

------------begin rodney output---------

Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.6 -- press F1 for help

>>> from rodney import suck
>>> suck(2000,6000,'cc')
>>> suck(2000.00,6000.00,'cc')
------------end rodney output------------

note the differences in the results!

I figured this out when I wrote the following lame C program:

----------lame c program-----------------
#include <stdio.h>
float rpm;
float cc;
float cid;
float cfm;
---------end lame c program---------------

which returns 180.105499
Is this a case of weak typing biting me on the backside, or is there a 
pythonic way to specify a float?