关闭ZeroDivisionError? [英] Turn off ZeroDivisionError?

查看:102
本文介绍了关闭ZeroDivisionError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想关闭ZeroDivisionError。我想要0./0。只要给NaN,

,输出时,只需打印''NaN''。我注意到fpconst有所需的

常量。我不想显着减慢浮点数学,所以我

不想只是捕获异常。


如果我使用C关闭硬件信号的代码,会阻止python从

检测到异常,或者是python检查它的分母是否为b
拥有(希望不是,这将浪费周期。

I''d like to turn off ZeroDivisionError. I''d like 0./0. to just give NaN,
and when output, just print ''NaN''. I notice fpconst has the required
constants. I don''t want to significantly slow floating point math, so I
don''t want to just trap the exception.

If I use C code to turn off the hardware signal, will that stop python from
detecting the exception, or is python checking for 0 denominator on it''s
own (hope not, that would waste cycles).

推荐答案

这里的包装函数是不可能的吗?


def MyDivision(num,denom):

if denom == 0:

return" NaN"

else

返回num / denom
Would a wrapper function be out of the question here?

def MyDivision(num, denom):
if denom==0:
return "NaN"
else
return num / denom


2月9日下午5:03 *,Neal Becker< ndbeck ... @ gmail.comwrote:
On Feb 9, 5:03*pm, Neal Becker <ndbeck...@gmail.comwrote:

如果我使用C代码关闭硬件信号,那么是否会从检测到异常的

停止python,或者是python检查0分母在它上面是b $ b拥有(希望不会,这会浪费周期)。
If I use C code to turn off the hardware signal, will that stop python from
detecting the exception, or is python checking for 0 denominator on it''s
own (hope not, that would waste cycles).



是的,Python确实对零分母进行了明确的检查。这里是

摘自于Objects / floatobject.c中的floatdiv.c:


if(b == 0.0){

PyErr_SetString(PyExc_ZeroDivisionError,float division);

返回NULL;

}


这可能是在进行浮动分割时,只有理智的方式来处理

平台行为的差异。

Yes, Python does do an explicit check for a zero denominator. Here''s
an excerpt from floatdiv.c in Objects/floatobject.c:

if (b == 0.0) {
PyErr_SetString(PyExc_ZeroDivisionError, "float division");
return NULL;
}

This is probably the only sane way to deal with differences in
platform behaviour when doing float divisions.


Mark Dickinson写道:
Mark Dickinson wrote:

2月9日下午5:03,Neal Becker< ndbeck ... @ gmail.comwrote:
On Feb 9, 5:03 pm, Neal Becker <ndbeck...@gmail.comwrote:

>如果我使用C代码关闭硬件信号,是否会阻止python检测异常,或者是python检查它自己的0分母(希望不会,这会浪费周期)。
>If I use C code to turn off the hardware signal, will that stop python from
detecting the exception, or is python checking for 0 denominator on it''s
own (hope not, that would waste cycles).



是的,Python确实对零分母进行了明确的检查。这里是

摘自于Objects / floatobject.c中的floatdiv.c:


if(b == 0.0){

PyErr_SetString(PyExc_ZeroDivisionError,float division);

返回NULL;

}


这可能是在做浮动分割时,只有理智的方式来处理

平台行为的差异。


Yes, Python does do an explicit check for a zero denominator. Here''s
an excerpt from floatdiv.c in Objects/floatobject.c:

if (b == 0.0) {
PyErr_SetString(PyExc_ZeroDivisionError, "float division");
return NULL;
}

This is probably the only sane way to deal with differences in
platform behaviour when doing float divisions.



你确定吗?


很可能是1 /(尽可能小的数字)>(最大的

可能的数字)。因此,除了陷阱之外,我还会捕获任何错误。

明显的零分区。

Are you sure?

It could very well be that 1/(smallest possible number)>(greatest
possible number). So I would also trap any errors besides trapping for
the obvious zero division.


这篇关于关闭ZeroDivisionError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆