[Tutor] Dividing 1 by another number ?
Terry Carroll
carroll at tjc.com
Mon Jan 31 00:39:10 CET 2005
On Sun, 30 Jan 2005, Brandon wrote:
> I'm trying to write a program they may involve needing to divide 1 by
> another number. In the program below when I use 4 for the diameter of
> the bore, and 1 for the diameter of the rod, and 60 for the PSI, the
> force should be 706.8 . However the program keeps giving me 0 for
> "rodarea".
In addition to the real division issue that's already been pointed out
(simplest fix is to divide by 4.0 instead of 4 to avoid this), you've got
another problem:
> area = (bore**2/4)*PI
> rodarea = (rod**2/4)*PI
Both of your formulas above are the same. So, where bore = rod, as in
your example where they're both equal to 1, area and rodarea are both
going to evaluate to the same value:
>>> bore = 1
>>> rod = 1
>>> PI = 3.14
>>> area = (bore**2/4.0)*PI
>>> rodarea = (rod**2/4.0)*PI
>>> area
0.78500000000000003
>>> rodarea
0.78500000000000003
Now, look at your nest line:
> force = (area-rodarea)*psi
since area is equal to rodarea when bore is equal to rod, area-rodarea
will always = 0, and force will equal 0, too, not 706.8 as you're
expecting.
This isn't just a matter of when you use 1, it's true whenever you use
equal values for rod and bore.
More information about the Tutor
mailing list