[Tutor] I need help with Python

Scott Widney SWidney at ci.las-vegas.nv.us
Thu Oct 9 21:03:05 EDT 2003


> I need to make calculations using very large numbers, 
> unfortunately, i am getting a "ZeroDivisionError: float 
> division" error.
> 
> Can you please help me out?
> 
> What follows is my program. Its simple and brief, yet it is 
> actually calculating one of the best proven theories in 
> science - special relativity.
> 
> 
> import cmath

Should be 'import math' -- you don't use any complex math in the code that
follows, but you do request the math.sqrt() function.

> 
> a = 186282.397
> b = 1609.344
> c = 365.2421987817
> d = 99.9999999999999999995

This is where you are getting caught. If you look at the value in 'd' here,
you'll see that it is stored as 100.0 -- your precision is lost.

> e = (d*a)/100

So here 'e' ends up being the same value of 'a',

> beta = ((e*b)**2) / ((a*b)**2)

and 'beta' ends up being the equivalent of 186282.397 divided by 186282.397,
namely 1.0

> gamma = 1/math.sqrt(1-beta)

So the calculation for 'gamma' ends up being: 1 / math.sqrt( 1 - 1.0). This
is where you are getting the exception. Do you see the problem?


Best wishes!
Scott



More information about the Tutor mailing list