15

In the code below: highly simplified. I get ZeroDivisionError: float division Any value below one gives errors. Other times 5/365 gives the error. How do I fix?

import math

def top(  t):
    return ((.3 / 2) * t) / (.3 * math.sqrt(t))


t = 365/365
top= top(t)
print (top)
6
  • 2
    @Merlin KerrekSB wants you to type 5/365, by itself, into the interpreter and see what the result is. Commented Sep 20, 2011 at 2:14
  • Type 5/365 into Python and see what it says. As the saying goes, teach a fish to debug... Commented Sep 20, 2011 at 2:15
  • 1
    @Merlin: "your method presupposes the knowledge "... Not true. It presupposes that one can use the Python interpreter. It doesn't require secret knowledge. Just the ability to type. Here's the point: try to type more and see what actually happens. That's the best way to debug. Commented Sep 20, 2011 at 2:25
  • 1
    @Merlin It would show you that it's not functioning as you expect. With that expectation removed, you would be empowered to search for the answer to why 5/365 returns 0. The biggest obstacle to debugging is our assumptions about how something works that turn out to be incorrect. Commented Sep 20, 2011 at 2:27
  • @Merlin: At the very least, if you type 1/2 and you get 0, it should make you think. With a little more luck, you'll deduce that you call sqrt(0) and thus divide by zero. Now you have a much better question, "why is 1/2 equal to 0?", and then you'll already hit one of the many SO questions to that end in the "possible duplicate" suggestions while you type the question... something along those lines. Commented Sep 20, 2011 at 2:29

3 Answers 3

12

The problem is here:

t = 365/365

You are dividing two integers, so python is using integer division. In integer division, the quotient is rounded down. For example, 364/365 would be equal to 0. (365/365 works because it is equal to 1, which is still 1 rounded down.)

Instead, use float division, like so.

t = 365.0/365.0
Sign up to request clarification or add additional context in comments.

2 Comments

This has changed in Python 3 -- in Python 3, you have to use // to get truncating division.
In fact it's not truncating division but floor division. python-history.blogspot.com.es/2010/08/…
9

In addition to cheeken's answer, you can put the following at the top of your modules:

from __future__ import division

Doing so will make the division operator work the way you want it to i.e always perform a (close approximation of) true mathematical division. The default behaviour of the division operator (where it performs truncating integer division if the arguments happen to be bound to integers) was inherited from C, but it was eventually realised that it was not a great fit for a dynamically typed language like Python. In Python 3, this no longer happens.

In my Python 2 modules, I almost always import division from __future__, so that I can't get caught out by accidentally passing integers to a division operation I don't expect to truncate.

It's worth noting that from __future__ import ... statements have to be the very first thing in your module (I think you can have comments and a docstring before it, nothing else). It's not really a normal import statement, even though it looks like one; it actually changes the way the Python interpreter reads your code, so it can't wait until runtime to be exectuted like a normal import statement. Also remember that import __future__ does not have any of the magic effects of from __future__ import ....

1 Comment

It's worth noting that import __future__ does not work the same way; you have to use the from version.
3

Try this:

 exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(stdev,2))))   

A ZeroDivisionError is encountered when you try to divide by zero.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.