草庐IT

int_void

全部标签

Python:让Python int像C int一样溢出

这个问题在这里已经有了答案:SimulatingintegeroverflowinPython(5个答案)关闭6年前。在Python中,当int大于2**31时,它会转为long:a=2147483647a+1=2147483648b=-2147483648b-1=-2147483649但我需要像C中的int一样的Pythonint溢出:a=2147483647a+1=-2147483648b=-2147483648b-1=2147483647这可能吗?提前致谢!

python - 如何将 pythons Decimal() 类型转换为 INT 和指数

我想在python中使用Decimal()数据类型并将其转换为整数和指数,这样我就可以将该数据发送到具有完全精度和小数控制的微Controller/plc。https://docs.python.org/2/library/decimal.html我已经让它工作了,但是它很老套;有谁知道更好的方法?如果不是,我会采取什么途径自己编写较低级别的“as_int()”函数?示例代码:fromdecimalimport*d=Decimal('3.14159')t=d.as_tuple()ift[0]==0:sign=1else:sign=-1digits=t[1]theExponent=t[2

Python Numpy : np. int32 "slower"比 np.float64

我想了解python的一个奇怪行为。让我们考虑一个矩阵M,其形状为6000x2000。该矩阵填充有符号整数。我想计算np.transpose(M)*M。两种选择:当我“自然地”执行此操作时(即没有指定任何类型),numpy选择类型np.int32并且该操作大约需要150秒。当我强制类型为np.float64(使用dtype=...)时,相同的操作大约需要2秒。我们如何解释这种行为?我天真地认为int乘法比float乘法便宜。非常感谢您的帮助。 最佳答案 不,整数乘法并不便宜。但稍后会详细介绍。很可能(我有99%的把握)numpy调用

python - int 和 numbers.Integral 在 Python 中的区别

我正在尝试更深入地了解Python的数据模型,但我没有完全理解以下代码:>>>x=1>>>isinstance(x,int)True>>>isinstance(x,numbers.Integral)True>>>inspect.getmro(int)(,)>>>inspect.getmro(numbers.Integral)(,,,,,)从上面看来,int和number.Integral似乎不在同一个层级。从Python引用(2.6.6)我看到numbers.Integral-Theserepresentelementsfromthemathematicalsetofintegers(

python - Flask: TypeError: 'int' 对象不可调用

这个问题在这里已经有了答案:FlaskviewraisesTypeError:'bool'objectisnotcallable(1个回答)关闭8年前。我是Python的新手,只是在做这个项目时学习它,这个问题真的让我很困惑。这是我的代码:fromflaskimportFlaskfromdatetimeimportdatetime#Setupappapp=Flask(__name__)#Initializedatadictdata={}#Pretty-formatsatimedifferencedefformatdifference(delta):seconds=delta.total

c++ - 如何在 Python ctypes 中处理 C++ 返回类型 std::vector<int>?

我找不到ctypes如何弥合std::vector和Python之间的差距;互联网上没有提到的组合。这是不好的做法,它不存在还是我遗漏了什么?C++:xxx.cpp#include#includeusingnamespacestd;extern"C"std::vectorfoo(constchar*FILE_NAME){stringline;std::vectorresult;ifstreammyfile(FILE_NAME);while(getline(myfile,line)){result.push_back(1);}return(result);}Python:xxx.pyim

python - 在 Python 3 中将字符串转换为 int 或 float?

integer=input("Number:")rslt=int(integer)+2print('2+'+integer+'='+rslt)double=input("PointNumber:")print('2.5+'+double+'='+(float(double)+2.5))给我Traceback(mostrecentcalllast):File"C:\...",line13,inprint('2+'+integer+'='+rslt)TypeError:Can'tconvert'int'objecttostrimplicitly我是编程新手,到目前为止,我的背景大多只是C#

python - 在 Python 中重载 int()

假设我在Python3中有一个基本类,它表示一些类似数字的数据类型。我想这样做,当我有一个此类的实例x时,我可以调用int(x)并让它调用我的转换函数以返回整数部分。我确信这很简单,但我似乎无法找到如何去做。 最佳答案 您按照以下示例覆盖__int__魔术方法...classTest:def__init__(self,i):self.i=idef__int__(self):returnself.i*2t=Test(5)print(int(t))#10 关于python-在Python中重

python - 如何在 Python 中将字符串转换为 int?

我的小示例应用程序的输出如下:WelcometotheCalculator!Pleasechoosewhatyou'dliketodo:0:Addition1:Subtraction2:Multiplication3:Division4:QuitApplication0Enteryourfirstnumber:1Enteryoursecondnumber:1Yourresultis:11这是因为addition()方法将input()作为字符串而不是数字。如何将它们用作数字?这是我的整个脚本:defaddition(a,b):returna+bdefsubtraction(a,b):r

python - 为什么numpy.core.numeric._typelessdata中有两个np.int64(为什么numpy.int64不是numpy.int64?)

这不像好奇心那么严重。在我的64位linux解释器中我可以执行In[10]:np.int64==np.int64Out[10]:TrueIn[11]:np.int64isnp.int64Out[11]:True太好了,正是我所期望的。但是我发现了numpy.core.numeric模块的这个奇怪的属性In[19]:fromnumpy.core.numericimport_typelessdataIn[20]:_typelessdataOut[20]:[numpy.int64,numpy.float64,numpy.complex128,numpy.int64]奇怪为什么numpy.in