Python 除以零等于零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27317517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make division by zero equal to zero
提问by octosquidopus
How can I ignore ZeroDivisionError
and make n / 0 == 0
?
我怎么能忽略ZeroDivisionError
并制作n / 0 == 0
?
采纳答案by davidism
Check if the denominator is zero before dividing. This avoids the overhead of catching the exception, which may be more efficient if you expect to be dividing by zero a lot.
在除法之前检查分母是否为零。这避免了捕获异常的开销,如果您希望除以零很多,这可能会更有效。
def weird_division(n, d):
return n / d if d else 0
回答by Cory Kramer
You can use a try
/except
block for this.
您可以为此使用try
/except
块。
def foo(x,y):
try:
return x/y
except ZeroDivisionError:
return 0
>>> foo(5,0)
0
>>> foo(6,2)
3.0
回答by Rick supports Monica
I think try
except
(as in Cyber's answer) is usually the best way (and more pythonic: better to ask forgiveness than to ask permission!), but here's another:
我认为try
except
(如 Cyber 的回答)通常是最好的方法(而且更像是 Python 语言:请求宽恕比请求许可更好!),但这是另一个:
def safe_div(x,y):
if y == 0:
return 0
return x / y
One argument in favor of doing it this way, though, is if you expect ZeroDivisionError
s to happen often, checking for 0 denominator ahead of time will be a lot faster (this is python 3):
不过,支持这样做的一个论点是,如果您希望ZeroDivisionError
s 经常发生,那么提前检查 0 分母会快得多(这是 python 3):
import time
def timing(func):
def wrap(f):
time1 = time.time()
ret = func(f)
time2 = time.time()
print('%s function took %0.3f ms' % (f.__name__, int((time2-time1)*1000.0)))
return ret
return wrap
def safe_div(x,y):
if y==0: return 0
return x/y
def try_div(x,y):
try: return x/y
except ZeroDivisionError: return 0
@timing
def test_many_errors(f):
print("Results for lots of caught errors:")
for i in range(1000000):
f(i,0)
@timing
def test_few_errors(f):
print("Results for no caught errors:")
for i in range(1000000):
f(i,1)
test_many_errors(safe_div)
test_many_errors(try_div)
test_few_errors(safe_div)
test_few_errors(try_div)
Output:
输出:
Results for lots of caught errors:
safe_div function took 185.000 ms
Results for lots of caught errors:
try_div function took 727.000 ms
Results for no caught errors:
safe_div function took 223.000 ms
Results for no caught errors:
try_div function took 205.000 ms
So using try
except
turns out to be 3 to 4 times slower for lots of (or really, all) errors; that is: it is 3 to 4 times slower for iterations that an error is caught. The version using the if
statement turns out to be slightly slower (10% or so) when there are few (or really, no) errors.
因此,try
except
对于许多(或实际上是所有)错误,使用结果会慢 3 到 4 倍;也就是说:捕获错误的迭代速度要慢 3 到 4 倍。if
当错误很少(或实际上没有)时,使用该语句的版本会稍微慢一些(10% 左右)。
回答by twasbrillig
def foo(x, y):
return 0 if y == 0 else x / y
回答by Totem
You can have a look to operator overload (https://docs.python.org/2/library/operator.html) if it's ok to wrap your integer in a new class. Then just check if y is 0 return 0 and x/y otherwise.
如果可以将整数包装在新类中,您可以查看运算符重载 ( https://docs.python.org/2/library/operator.html)。然后只需检查 y 是否为 0 返回 0,否则返回 x/y。
回答by Vynylyn
I think if you don't want to face Zer0DivErrr, you haven't got to wait for it or go through it by using try-except expression. The quicker way is to jump over it by making your code simply not to do division when denominator becomes zero:
我想如果你不想面对 Zer0DivErrr,你不必等待它或通过使用 try-except 表达式来完成它。更快的方法是跳过它,让您的代码在分母变为零时简单地不进行除法:
(if Y Z=X/Y else Z=0)
回答by Saurav Panda
You can use the following :
您可以使用以下内容:
x=0,y=0
print (y/(x or not x))
Output:
输出:
>>>x=0
>>>y=0
>>>print(y/(x or not x))
0.0
>>>x =1000
>>>print(y/(x or not x))
0.000999000999000999
not x will be false if x is not equal to 0, so at that time it divides with actual x.
如果 x 不等于 0,则 not x 将是假的,因此此时它与实际 x 相除。