[Tutor] I need help with Python
Jeff Shannon
jeff at ccvcorp.com
Thu Oct 9 20:50:29 EDT 2003
Vickram wrote:
> I need to make calculations using very large numbers, unfortunately, i
> am getting a "ZeroDivisionError: float division" error.
This would be easier to determine if you had included the rest of the
traceback, which would tell us exactly *where* the error is. But
let's see what we can do.
The error means that you're dividing by zero. Mathematics tells us
that this yields an undefined result, but computers can't handle
undefined numbers, so it's not allowed and is considered an error.
The trick is to find out where in your program you might be dividing
by zero.
> import cmath
>
> a = 186282.397
> b = 1609.344
> c = 365.2421987817
> d = 99.9999999999999999995
> e = (d*a)/100
> beta = ((e*b)**2) / ((a*b)**2)
> gamma = 1/math.sqrt(1-beta)
All of this should be good. You've got a set of floating point
numbers, none of which approximate zero all that closely.
> ty = 100000
> td = ty*c
> print
> print "speed of light =",a,"mps"
> print "1 mile =",b,"metres"
> print "1 year =",c,"days"
> print
> print "% of c =",d,"%"
> print "gamma =",gamma
> print
> print "normal time = ",ty,"years"
> print " ",td,"days"
> print
> print "time dilation =",ty/round(gamma),"yrs"
Here's where I think your problem is. You're rounding gamma to the
nearest integer, and then dividing by it. You previously defined
gamma as a fraction -- it's somewhere between 0 and 1. If gamma
happens to be closer to 0, then round(gamma) will be equal to 0, and
there you've got an illegal division by zero.
I'm not sure why you'd want to round off gamma for this calculation,
anyhow. Perhaps you meant to round off the results of ty/gamma, instead?
Jeff Shannon
Technician/Programmer
Credit International
More information about the Tutor
mailing list