Python Float Division Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site float division by zero python error About Us Learn more about Stack Overflow the company Business Learn more
Zerodivisionerror: Float Divmod()
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss
Zerodivisionerror: Division By Zero Python
Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Python float division
Zerodivisionerror Python
“rounding error” on division by 100 [duplicate] up vote 0 down vote favorite This question already has an answer here: Limiting floats to two decimal points 15 answers Is floating point math broken? 27 answers When dividing a float by 100 in Python 2.7 I get the following "rounding behaviour": >>> 3.7e-03/100 3.7000000000000005e-05 I would expect the following: >>> 3.7e-03/100 3.7e-05 Note that: >>> 3.7e-03/100 how to handle division by zero python == 3.7e-05 False >>> 3.7000000000000005e-05 == 3.7e-05 False While probably of not practical difference in most applications I find this behaviour somewhat disconcerting. Why does this happen and how can I avoid it? I am using Python: '2.7.5 |Anaconda 1.7.0 (32-bit)| (default, Jul 1 2013, 12:41:55) [MSC v.1500 32 bit (Intel)]' python python-2.7 floating-point division floating-accuracy share|improve this question edited Oct 31 '13 at 14:25 asked Oct 31 '13 at 10:13 ARF 2,64011631 marked as duplicate by geoffspear, Daniel Roseman, Henrik, FallenAngel, Martijn Pieters♦ Oct 31 '13 at 10:33 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. This has a good explanation stackoverflow.com/questions/5997027/… –Niek de Klein Oct 31 '13 at 10:15 What happens if you don't divide: i.e. what is 3.7e-03? The trouble is first computers use binary and 10 isn't a power of two. Second they only have limited bytes, so even if something could be written as the sum of exact powers of two you might get rounding errors due to lack of bytes. –doctorlove Oct 31 '13
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the python float precision company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions float in python Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million python cast to float programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Get ZeroDivisionError: float division in python up vote 2 down vote favorite 1 In the code below: highly simplified. I get http://stackoverflow.com/questions/19703657/python-float-division-rounding-error-on-division-by-100 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 python share|improve this question asked Sep 20 '11 at 2:04 Merlin 5,1891851119 What's the value of 5/365? –Kerrek SB Sep 20 '11 at 2:08 Does Python tell you http://stackoverflow.com/questions/7479292/get-zerodivisionerror-float-division-in-python this? –Kerrek SB Sep 20 '11 at 2:10 2 @Merlin KerrekSB wants you to type 5/365, by itself, into the interpreter and see what the result is. –cheeken Sep 20 '11 at 2:14 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. –S.Lott Sep 20 '11 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. –Ben Sep 20 '11 at 2:27 | show 3 more comments 2 Answers 2 active oldest votes up vote 5 down vote accepted 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 share|imp
base 2 (binary) fractions. For example, the decimal fraction 0.125 has value 1/10 + 2/100 + 5/1000, and in the same way the binary fraction https://docs.python.org/3/tutorial/floatingpoint.html 0.001 has value 0/2 + 0/4 + 1/8. These two fractions have identical values, the only real difference being that the first is written in base 10 fractional notation, and the second in base 2. Unfortunately, most decimal fractions cannot be represented exactly as binary fractions. A consequence is that, in general, the decimal floating-point numbers you division by enter are only approximated by the binary floating-point numbers actually stored in the machine. The problem is easier to understand at first in base 10. Consider the fraction 1/3. You can approximate that as a base 10 fraction: 0.3 or, better, 0.33 or, better, 0.333 and so on. No matter how many digits you're willing to write down, division by zero the result will never be exactly 1/3, but will be an increasingly better approximation of 1/3. In the same way, no matter how many base 2 digits you're willing to use, the decimal value 0.1 cannot be represented exactly as a base 2 fraction. In base 2, 1/10 is the infinitely repeating fraction 0.0001100110011001100110011001100110011001100110011... Stop at any finite number of bits, and you get an approximation. On most machines today, floats are approximated using a binary fraction with the numerator using the first 53 bits starting with the most significant bit and with the denominator as a power of two. In the case of 1/10, the binary fraction is 3602879701896397 / 2 ** 55 which is close to but not exactly equal to the true value of 1/10. Many users are not aware of the approximation because of the way values are displayed. Python only prints a decimal approximation to the true decimal value of the binary approximation stored by the machine. On most machines