如何强制在 Python 中对减法进行签名?

发布于 2024-08-03 07:49:03 字数 621 浏览 11 评论 0原文

如果你不关心背景,你可以跳到最后一行:

我在Python中有以下代码:

ratio = (point.threshold - self.points[0].value) / (self.points[1].value - self.points[0].value)

这给了我错误的值。例如,对于:

threshold:  25.0
self.points[0].value:  46
self.points[1].value:  21

我得到:

ratio:  -0.000320556853048

这是错误的。

仔细研究一下,我意识到 self.points[0].value 和 self.points[1].value] 的类型是 numpy.uint16`,所以我得到:

21 - 46 = 65511

虽然我从未定义过 point.threshold 的类型。我刚刚分配了它。我想它有一个普通的int

底线

如何强制对两个 uint 的减法进行签名?

You can skip to the bottom line if you don't care about the background:

I have the following code in Python:

ratio = (point.threshold - self.points[0].value) / (self.points[1].value - self.points[0].value)

Which is giving me wrong values. For instance, for:

threshold:  25.0
self.points[0].value:  46
self.points[1].value:  21

I got:

ratio:  -0.000320556853048

Which is wrong.

Looking into it, I realized that self.points[0].value and self.points[1].value] are of the typenumpy.uint16`, so I got:

21 - 46 = 65511

While I never defined a type for point.threshold. I just assigned it. I imagine it got a plain vanilla int.

The Bottom Line

How can I force the the subtraction of two uints to be signed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝對不後悔。 2024-08-10 07:49:03

几乎任何 uint 都可以在这里使用,因此在进行减法之前只需将它们转换为其他值即可。

由于阈值 = 25.0(注意小数点),它是一个浮点数,因此只要不使用 uint,减法和除法都将起作用。

Almost anything but uints will work here, so just cast these to something else before you do the subtraction.

Since threshold = 25.0 (note the decimal point), it's a float, so the subtraction and division will all work as long as you're not using uints.

楠木可依 2024-08-10 07:49:03

好吧,明显的解决方案可能是转换为浮点数:

ratio = (float(point.threshold) - float(self.points[0].value)) / (float(self.points[1].value) - float(self.points[0].value))

或者我想您可以转换为 numpy 有符号类型之一。

Well, the obvious solution would probably be to cast to floats:

ratio = (float(point.threshold) - float(self.points[0].value)) / (float(self.points[1].value) - float(self.points[0].value))

Or I suppose you could cast to one of the numpy signed types.

倾城泪 2024-08-10 07:49:03

这些值实际上需要是 uint16 而不是 int16 吗?除非它们必须能够接受 2**15 或以上的值(但仍低于 2**16),否则您可以简单地将它们保留为 int16 并完成它——正如您所发现的,无符号整数可能很棘手(不仅仅是在 numpy 中;-)。如果您确实需要 uint16,那么按照 David 的建议进行强制转换就可以了,但如果您可以简单地使用 int16,它将更快、更具可读性。

顺便说一句,看起来 point.threshold 是一个浮点数,而不是一个整数(这也是一件好事,否则你编码的除法将是一个截断的除法,除非你从未来导入真正的除法,正如所支持的那样在 Python 的许多 2.* 版本中 - 并且最终是除法在 3.* 中的工作方式)。 25.0 中的 .0 “泄露了它”并显示它是浮点数,而不是整数。

Do those values actually NEED to be uint16 instead of int16? Unless they have to be able to take values of 2**15 or above (but still below 2**16) you could simply keep them as int16 and be done with it -- unsigned ints, as you discovered, can be tricky (and not just in numpy;-). If you DO need the uint16, then casts as David suggests will work, but if you can simply use int16 it will be faster and more readable.

BTW, it looks like that point.threshold is a float, not an int (good thing too, otherwise that division the way you code it would be a truncating one, unless you're importing true division from the future, as has been supported in many 2.* releases of Python -- and is finally THE way division works in 3.*). The .0 in 25.0 "gives it away" and shows it's a float, not an int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文