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.
我正在对字典进行一些计算。但重要的是我想做一个 if-condition 说
“如果x的值比变量更接近 1(或等于 1) ”
有点难以解释,但希望你能理解。
您可以使用绝对值:
if abs(x-1) < abs(variable-1): ...
因为 的绝对值是和x-1之间的距离,同样 的绝对值是和之间的距离。x1variable-1variable1
x-1
x
1
variable-1
variable