我在sympy中得到了非常奇怪的部分。
我不知道为什么它给了我这些不寻常的分数
I get very weird fractions in sympy.
I have no clue why it is given me these unusual fractions
回答您的问题:
Rational("0.1") # out: 1 / 10 Rational(0.1) # out: 3602879701896397/36028797018963968
第二个输出发生是因为0.1是许多类型的浮点,因此它本质上是不准确的。
0.1
To answer your question:
The second output happens because 0.1 is a number of type float, hence it is inherently inaccurate.
Sympy手册: https://docs.sympy.org/latest/explanation/gotchas.html#pypython-numbers-vs-sympy-numbers
简
而,然后Sympy必须测试浮子。
但是Rational(“ 0.1”)有效。 str保留数字。
Rational(“ 0.1”)
如果您不喜欢这个。
首先将其转换为Sympy的类型整数(1)/10。也有效。
整数(1)/10
sympy manual: https://docs.sympy.org/latest/explanation/gotchas.html#python-numbers-vs-sympy-numbers
in short:
you type Rational(0.1), then sympy has to test a float.
Rational(0.1)
but Rational("0.1") works. str keep the number.
Rational("0.1")
if you don't like this.
First convert it to sympy's type Integer(1)/10. Also works.
Integer(1)/10
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(2)
回答您的问题:
第二个输出发生是因为
0.1
是许多类型的浮点,因此它本质上是不准确的。To answer your question:
The second output happens because
0.1
is a number of type float, hence it is inherently inaccurate.Sympy手册: https://docs.sympy.org/latest/explanation/gotchas.html#pypython-numbers-vs-sympy-numbers
简
而,然后Sympy必须测试浮子。
但是
Rational(“ 0.1”)
有效。 str保留数字。如果您不喜欢这个。
首先将其转换为Sympy的类型
整数(1)/10
。也有效。sympy manual: https://docs.sympy.org/latest/explanation/gotchas.html#python-numbers-vs-sympy-numbers
in short:
you type
Rational(0.1)
, then sympy has to test a float.but
Rational("0.1")
works. str keep the number.if you don't like this.
First convert it to sympy's type
Integer(1)/10
. Also works.