问题描述
我想关闭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).
推荐答案
是的,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.
是的,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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!