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'相关推荐
- Go 学习笔记(32)— 类型系统(命名类型、未命名类型、底层类型、类型强制转换、类型别名和新声明类型)
1. 命名类型和未命名类型 1.1 命名类型 类型可以通过标识符来表示,这种类型称为命名类型( Named Type ). Go 语言的基本类型中有 20 个预声明简单类型都是命名类型, Go 语言还 ...
- typescript索引类型_TypeScript类型声明书写详解
本文总结一下TypeScript类型声明的书写,很多时候写TypeScript不是问题,写类型就特别纠结,我总结下,我在使用TypeScript中遇到的问题.如果你遇到类型声明不会写的时候,多看看lo ...
- 【Kotlin】函数类型 ( 函数类型 | 带参数名称的参数列表 | 可空函数类型 | 复杂函数类型 | 带接收者函数类型 | 函数类型别名 | 函数类型实例化 | 函数调用 )
文章目录 I . 函数类型 II . 带参数名的参数列表 III . 可空函数类型 IV . 复杂函数类型解读 V . 函数类型别名 VI . 带 接收者类型 的函数类型 VII . 函数类型实例化 ...
- [你必须知道的.NET]第九回:品味类型---值类型与引用类型(中)-规则无边
发布日期:2007.5.28 作者:Anytao ©2007 Anytao.com ,原创作品,转贴请注明作者和出处. 接上回[第八回:品味类型---值类型与引用类型(上)-内存有理]的探讨,继续我们 ...
- 第八回:品味类型---值类型与引用类型(上)-内存有理
第八回:品味类型---值类型与引用类型(上)-内存有理 http://www.cnblogs.com/anytao/archive/2007/05/23/must_net_08.html 发布日期:2 ...
- Scala 类型的类型(一)
目录 1. Scala 类型的不同类型 2. 写作进度 3. Type Ascription 4. 通用类型系统 - Any, AnyRef, AnyVal 5. 底类型 - Nothing 与 Nu ...
- ts定义html是什么类型,TypeScript—类型定义文件(*.d.ts)
一.ts文件中引入jquery. 1.大家是否有再vue 上使用过 ts,并再 .ts文件中引用过 jquery 1.1是不是遇到过如下问题: import $ from 'jquery'; /*** ...
- [你必须知道的.NET]第十回:品味类型---值类型与引用类型(下)-应用征途
本文将介绍以下内容: 类型的基本概念 值类型深入 引用类型深入 值类型与引用类型的比较及应用 [下载]:[类型示例代码] 1. 引言 值类型与引用类型的话题经过了两个回合([第八回:品味类型---值类 ...
- [你必须知道的.NET] 第八回:品味类型---值类型与引用类型(上)-内存有理
本文将介绍以下内容: 类型的基本概念 值类型深入 引用类型深入 值类型与引用类型的比较及应用 1. 引言 买了新本本,忙了好几天系统,终于开始了对值类型和引用类型做个全面的讲述了,本系列开篇之时就是因 ...
最新文章
- netbios 网上基本输入输出系统 简介
- 计算机在线采集数据注意,全站仪数据采集和传输中的常见问题解决方案
- java formatter()_Java Formatter locale()用法及代码示例
- ActionContext和ActionSupport的学习
- AngularJS 使用 Promise
- “易语言.飞扬”十分钟入门教程(修订版1,update for EF1.1.0)
- http 标准超时时间_Go 中 http 超时问题的排查
- Mysql经常使用函数汇总
- jenkins 手动执行_Jenkins环境配置篇-节点增加
- 学科竞赛作品 智能家居受控端
- 捷联惯导系统模型及仿真(二)
- 职业规划范文500字计算机专业,技校计算机专业职业生涯规划500字左右
- 图像与视频的Alpha通道
- 【转帖】GBase 数据库
- linux 防火墙firewall规则策略
- nbu新增media server过程简介
- 腾达ap设置说明_腾达(Tenda)F3无线信号放大模式(Client+AP)设置 | 192路由网
- Python同步学习(一)
- 什么是自动气象站 校园气象站
- 容器云 docker