日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

long类型python_Python类型long vs C'long long'

發布時間:2024/7/5 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 long类型python_Python类型long vs C'long long' 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

I would like to represent a value as a 64bit signed long, such that values larger than (2**63)-1 are represented as negative, however Python long has infinite precision. Is there a 'quick' way for me to achieve this?

解決方案>>> from ctypes import c_longlong as ll

>>> ll(2 ** 63 - 1)

c_longlong(9223372036854775807L)

>>> ll(2 ** 63)

c_longlong(-9223372036854775808L)

>>> ll(2 ** 63).value

-9223372036854775808L

This is really only an option if you know for sure that a signed long long will be 64 bits wide on the target machine(s).

Edit: jorendorff's idea of defining a class for 64 bit numbers is appealing. Ideally you want to minimize the number of explicit class creations.

Using c_longlong, you could do something like this (note: Python 3.x only!):

from ctypes import c_longlong

class ll(int):

def __new__(cls, n):

return int.__new__(cls, c_longlong(n).value)

def __add__(self, other):

return ll(super().__add__(other))

def __radd__(self, other):

return ll(other.__add__(self))

def __sub__(self, other):

return ll(super().__sub__(other))

def __rsub__(self, other):

return ll(other.__sub__(self))

...

In this way the result of ll(2 ** 63) - 1 will indeed be 9223372036854775807. This construction may result in a performance penalty though, so depending on what you want to do exactly, defining a class such as the above may not be worth it. When in doubt, use timeit.

總結

以上是生活随笔為你收集整理的long类型python_Python类型long vs C'long long'的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。