Sympy中的非常不寻常的分数,如何解决这个问题?

发布于 2025-02-12 17:26:58 字数 369 浏览 0 评论 0原文

我在sympy中得到了非常奇怪的部分。

我不知道为什么它给了我这些不寻常的分数

I get very weird fractions in sympy.
enter image description here

I have no clue why it is given me these unusual fractions

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掌心的温暖 2025-02-19 17:26:58

回答您的问题:

Rational("0.1")
# out: 1 / 10
Rational(0.1)
# out: 3602879701896397/36028797018963968

第二个输出发生是因为0.1是许多类型的浮点,因此它本质上是不准确的。

To answer your question:

Rational("0.1")
# out: 1 / 10
Rational(0.1)
# out: 3602879701896397/36028797018963968

The second output happens because 0.1 is a number of type float, hence it is inherently inaccurate.

空心空情空意 2025-02-19 17:26:58

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文