Python中C类型整数的最大值和最小值 [英] Maximum and minimum value of C types integers from Python

查看:100
本文介绍了Python中C类型整数的最大值和最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种获取(使用Python)C类型整数的最大值和最小值的方法(即uint8int8uint16int16uint32int32uint64int64 ...).

I'm looking for a way to get (using Python) the maximum and minimum values of C types integers (ie uint8, int8, uint16, int16, uint32, int32, uint64, int64...) from Python.

我希望在ctypes模块中找到它

In [1]: import ctypes
In [2]: ctypes.c_uint8(256)
Out[2]: c_ubyte(0)
In [3]: ctypes.c_uint8(-1)
Out[3]: c_ubyte(255)

但我找不到它.

朱莉娅(Julia)具有出色的功能:

Julia have great feature for this:

julia> typemax(UInt8)
0xff

julia> typemin(UInt8)
0x00

julia> typemin(Int8)
-128

julia> typemax(Int8)
127

我很确定Python有类似的东西.

I'm pretty sure Python have something quite similar.

理想情况下,我什至在寻找一种方法来确保可以将给定的Python整数(据说是无界的)安全地转换为给定大小的C类型整数. 如果数字不在预期的间隔内,则应引发异常.

Ideally I'm even looking for a way to ensure that a given Python integer (which is said to be unbounded) can be converted safely in a C type integer of a given size. When number is not in expected interval, it should raise an exception.

当前溢出不会引发异常:

Currently overflow doesn't raise exception:

In [4]: ctypes.c_uint8(256)
Out[4]: c_ubyte(0)

我看到了这样的帖子整数的最大值和最小值,但这是与作者寻找Python整数的最小值/最大值有点不同...不是来自Python的C整数

I saw this SO post Maximum and Minimum values for ints but it's a bit different as author is looking for min/max value of a Python integer... not a C integer (from Python)

我还注意到检测C类型限制("limits.h" ;)在python中?但是,即使它很相关,也无法真正回答我的问题.

I also noticed Detecting C types limits ("limits.h") in python? but, even if it's quite related, it doesn't really answer my question.

推荐答案

根据:

整数具有无限精度.

Integers have unlimited precision.

翻译成代码:

>>> i = 10 ** 100
>>> i
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>>> len(str(i))
101
>>> i.bit_length()
333

另一方面,每种 C 类型都有固定的大小(取决于平台/体系结构),如

On the other hand, each C type has a fixed size (depending on platform / architecture), as clearly shown in [CppReference]: Fundamental types.

由于 [Python 3.Docs]:ctypes-外部函数用于Python的库没有提及任何有关类型限制的内容(请注意,此处未记录某些内容),让我们手动进行查找.

Since [Python 3.Docs]: ctypes - A foreign function library for Python doesn't mention anything about types limits (note that there is some stuff not documented here), let's find that out manually.

code.py :

#!/usr/bin/env python3

import sys
from ctypes import c_int8, c_uint8, c_byte, c_ubyte, c_int16, c_uint16, \
    c_int32, c_uint32, c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong, \
    c_int64, c_uint64, \
    sizeof


def limits(c_int_type):
    signed = c_int_type(-1).value < c_int_type(0).value
    bit_size = sizeof(c_int_type) * 8
    signed_limit = 2 ** (bit_size - 1)
    return (-signed_limit, signed_limit - 1) if signed else (0, 2 * signed_limit - 1)


def main():
    test_types = [
        c_int8,
        c_uint8,
        c_byte,
        c_ubyte,
        c_int16,
        c_uint16,
        c_int32,
        c_uint32,
        c_int,
        c_uint,
        c_long,
        c_ulong,
        c_longlong,
        c_ulonglong,
        c_int64,
        c_uint64
    ]
    for test_type in test_types:
        print("{:s} limits: ({:d}, {:d})".format(test_type.__name__, *limits(test_type)))


if __name__ == "__main__":
    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
    main()

注释:

  • 代码依赖于以下事实:对于某个整数类型,其间隔(和限制是间隔的端点)为:
    • 签名 ( 2 的补语) : [-(2 bit_size-1 ),2 bit_size-1 -1]
    • 未签名: [0,2 bit_size -1]
    • Code relies on the fact that for a certain integral type, its interval (and limits are interval's endpoints) is:
      • signed (2's complement): [-(2 bit_size - 1), 2 bit_size - 1 - 1]
      • unsigned: [0, 2 bit_size - 1]

      输出:

      (py35x64_test) e:\Work\Dev\StackOverflow\q052475749>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code.py
      Python 3.5.4 (v3.5.4:3f56838, Aug  8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
      
      c_byte limits: (-128, 127)
      c_ubyte limits: (0, 255)
      c_byte limits: (-128, 127)
      c_ubyte limits: (0, 255)
      c_short limits: (-32768, 32767)
      c_ushort limits: (0, 65535)
      c_long limits: (-2147483648, 2147483647)
      c_ulong limits: (0, 4294967295)
      c_long limits: (-2147483648, 2147483647)
      c_ulong limits: (0, 4294967295)
      c_long limits: (-2147483648, 2147483647)
      c_ulong limits: (0, 4294967295)
      c_longlong limits: (-9223372036854775808, 9223372036854775807)
      c_ulonglong limits: (0, 18446744073709551615)
      c_longlong limits: (-9223372036854775808, 9223372036854775807)
      c_ulonglong limits: (0, 18446744073709551615)
      

      这篇关于Python中C类型整数的最大值和最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆