Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我定义了一个这样的分段函数:
def C1(d,x): if ((x<d/2) and (x>-d/2)): return (1/d) else: return 0
但它总是返回我 0
In [23]: C1(1000,1) Out[23]: 0
尝试from __future__ import division
from __future__ import division
这是解释这一点的 PEP 238 的链接。
https://www.python.org/dev/peps/pep-0238/
在 Python 中,除号运算符 (/) 执行整数除法,这意味着如果分子和分母都是整数,则任何小数点都会被截断。因此,当您执行 (1/d) 时,1 和 d 都是整数,因此它确实如此,例如,1/1000 是 0.001,但在整数除法中您会丢失小数,因此您会得到 0。要解决此问题,请将所有您定义的数字通过添加 .0 浮动。例如,(x < d/2.0), and (x > -d/2.0) and (1.0/d) 这应该会给你正确的结果。
I have the following code:
document.getElementById("launcherDiv").innerHTML = "<object id='launcher' classid='CLSID:17E88883-3488-46B8-BE4D-30568888855' codebase='Helper.CAB#version=8