当我使用ctypes.c_int()时,它返回一个不同的数字?

huangapple go评论86阅读模式
英文:

When I use ctypes.c_int() it returns a different number?

问题

当我使用下面的代码时,它返回一个不同的数字。

numb = 5000000000
n = ctypes.c_int(numb)

它转换的数字是:705032703

更新

现在它不会报错,并执行了操作。然而,它返回的数字仍然不同。它应该返回我输入的数字,但它没有返回。

main.py

numb = 5000000000
n = ctypes.c_int64(numb)
x = hello_world(n)
print(x)

我将Golang代码转换为C代码

main.go

package main

import "C"

func helloWorld(x int64) int64 {
    s := int64(1)
    for i := int64(1); i < x; i++ {
        s = i
    }
    return s
}
英文:

> When I use the code below, it returns a different number.
>
>
> numb = 5000000000
> n = ctypes.c_int(numb)
>
> the number it converts : 705032703

UPDATE

now it doesn't give error. and performed the operation. However, the number it returns is still different. It should return the number I entered. but it does not return.

main.py

numb = 5000000000
n = ctypes.c_int64(numb)
x = hello_world(n)
print(x)

golang code that I converted to c code

main.go

package main

import &quot;C&quot;

func helloWorld(x int64) int64 {
	s := int64(1)
	for i := int64(1); i &lt; x; i++ {
		s = i
	}
	return s
 }

答案1

得分: 2

5,000,000,000对于32位整数来说太大了。你可以看到下面的示例中它被截断为32位:

>>> n=5000000000
>>> hex(n)
'0x12a05f200'         # 33位
>>> import ctypes
>>> hex(ctypes.c_int(n).value)
'0x2a05f200'          # 去掉了第33位

你也可以通过与32位掩码进行按位与操作来看到相同的结果:

>>> n & 0xffffffff
705032704
英文:

5,000,000,000 is too large for a 32-bit integer. You can see below it just truncates to 32 bits:

&gt;&gt;&gt; n=5000000000
&gt;&gt;&gt; hex(n)
&#39;0x12a05f200&#39;         # 33 bits
&gt;&gt;&gt; import ctypes
&gt;&gt;&gt; hex(ctypes.c_int(n).value)
&#39;0x2a05f200&#39;          # dropped the 33rd bit

You can see the same thing by ANDing with a 32-bit mask:

&gt;&gt;&gt; n &amp; 0xffffffff
705032704

答案2

得分: 1

ctypes暴露了与C兼容的类型,其中ctypes.c_int()是有符号int类型的一种暴露,根据平台的不同(甚至可能更窄或更宽),它可以是32位或64位。

在您的实例中,您的c_int是32位类型,32位有符号整数可以保存范围[-2147483648, 2147483647],或者有4294967296个可能的值。

因此,它无法直接保存您的5000000000(50亿),并且溢出处理方式是由实现定义的。

如果您想保存大数,建议使用固定宽度类型,如c_int64,这样您就可以确切地知道自己在处理什么,因为c_intc_long具有一些大小保证,但在其他方面是实现/平台特定的。

英文:

ctypes exposes the C-compatible types, with ctypes.c_int() being an exposure of the signed int type, which could be 32-bit or 64-bit depending on the platform (or possibly even narrower or wider).

In your instance, your c_int is a 32-bit type, and a 32-bit signed int can hold the range [-2147483648, 2147483647], or 4294967296 possible values.

So it can't hold your 5000000000 (5 billion) as-is, and it's implementation-defined how overflow is handled.

If you want to hold large numbers, it's recommended to used fixed-width types like c_int64 so you know exactly what you're dealing with, as c_int and c_long have some size guarantees but are otherwise implementation/platform specific.

huangapple
  • 本文由 发表于 2022年8月18日 01:23:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/73392606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定