如何在sympy(Python)中将float转换为int?

时间:2022-06-24 18:19:42

I have a sympy Float constant, e.g,

我有一个同情的浮动常数,例如,

x=Float(3.2)

I need to transform the floats to integers, i.e,

我需要将浮点数转换为整数,即

x+Int(3)

Is there any built-in method in sympy that can transform Float(3.2) to Int(3)?

在sympy中是否有任何可以将Float(3.2)转换为Int(3)的内置方法?

2 个解决方案

#1


3  

Casting a floating-point number to a SymPy integer:

将浮点数转换为SymPy整数:

In [1]: a = 3.2

In [2]: b = Integer(a)

In [3]: b
Out[3]: 3

SymPy integers behave mathematically under division:

SymPy整数在划分下以数学方式表现:

In [4]: b/2
Out[4]: 3/2

Casting the same floating-point number to a Python integer:

将相同的浮点数转换为Python整数:

In [5]: c = int(a)

In [6]: c  
Out[6]: 3

In Python 3 or using future division, you can get back a floating point:

在Python 3中或使用将来的除法,您可以获得一个浮点:

In [7]: c/2
Out[7]: 1.5

#2


3  

Just use the Integer() method. It allows to convert the float to int. Integer() always brings the float down to a round figure.

只需使用Integer()方法。它允许将float转换为int。 Integer()总是将浮点数降低到圆形数字。

Example from interpreter:

解释器示例:

>>> a = 3.2
>>> b = Integer(a)
>>> b
3

#1


3  

Casting a floating-point number to a SymPy integer:

将浮点数转换为SymPy整数:

In [1]: a = 3.2

In [2]: b = Integer(a)

In [3]: b
Out[3]: 3

SymPy integers behave mathematically under division:

SymPy整数在划分下以数学方式表现:

In [4]: b/2
Out[4]: 3/2

Casting the same floating-point number to a Python integer:

将相同的浮点数转换为Python整数:

In [5]: c = int(a)

In [6]: c  
Out[6]: 3

In Python 3 or using future division, you can get back a floating point:

在Python 3中或使用将来的除法,您可以获得一个浮点:

In [7]: c/2
Out[7]: 1.5

#2


3  

Just use the Integer() method. It allows to convert the float to int. Integer() always brings the float down to a round figure.

只需使用Integer()方法。它允许将float转换为int。 Integer()总是将浮点数降低到圆形数字。

Example from interpreter:

解释器示例:

>>> a = 3.2
>>> b = Integer(a)
>>> b
3