maxima CAS:替换了浮动鼠

发布于 2024-11-13 14:56:36 字数 206 浏览 3 评论 0原文

在 Maxima 我这样做:

(%i1) 1.4*28;

(%o1) 39.2

(%i2) is(1.4*28=39.2);

(%o2) false

这对我来说很奇怪,但可能与老鼠替换有关?

有没有办法让 maxima 返回 'true' 的输入 是(1.4*28=39.2);

in Maxima I do:

(%i1) 1.4*28;

(%o1) 39.2

(%i2) is(1.4*28=39.2);

(%o2) false

This is strange to me, but probably has to do with rat replace?

Is there a way to let maxima return 'true' to the input of
is(1.4*28=39.2);?

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

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

发布评论

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

评论(2

挽清梦 2024-11-20 14:56:36

来自浮点指南

为什么我的数字(例如 0.1 + 0.2)加起来不等于 0.3,并且
相反,我得到了一个奇怪的结果,比如
0.30000000000000004?

因为计算机内部使用
格式(二进制浮点)
不能准确地表示一个数字
就像 0.1、0.2 或 0.3 一样。

当代码被编译或
解释后,你的“0.1”已经是
四舍五入到最接近的数字
格式,这会导致一个小
甚至在之前的舍入误差
计算发生。

在您的情况下,1.4 和 39.2 都不能完全表示为二进制分数,并且计算结果最终的舍入方式与文字 39.2 不同。

如果您想避免此类问题,则必须避免使用二进制浮点数。我认为在 Maxima 中,通过使用真分数最容易做到这一点:

is(14/10 * 28 = 392/10)

应该有效

From The Floating-Point Guide:

Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and
instead I get a weird result like
0.30000000000000004?

Because internally, computers use a
format (binary floating-point) that
cannot accurately represent a number
like 0.1, 0.2 or 0.3 at all.

When the code is compiled or
interpreted, your “0.1” is already
rounded to the nearest number in that
format, which results in a small
rounding error even before the
calculation happens.

In your case, both 1.4 and 39.2 are not exactly representable as a binary fraction and the result of the computation ends up being rounded differently than the literal 39.2.

If you want to avoid such issues, you'll have to avoid the use of binary floats. I think in Maxima, this is most easily done by using proper fractions:

is(14/10 * 28 = 392/10)

should work

以歌曲疗慰 2024-11-20 14:56:36

由于“常规”计算器会出错,因此有时并不清楚什么才能真正解决您的问题。如果您正在设置自动算术测试来查看人们是否可以获得正确的答案,那么也许您应该测试的是在一定容忍度范围内的一致性。

(计算器的示例可以通过计算 1.0/3.0 的变体然后乘以 3 来构建。您可能会得到 0.9999...。有些计算器比其他计算器舍入得更好,因此示例必须稍微微妙一些。例如 1.0/30.0 X 30.0)

回到Maxima,你可以测试一下abs(ab)<是否>宽容,或者也许
绝对值((ab)/最大(a,b)) <相对耐受性。

现在,如果真正解决您问题的只是降低输出精度,您可以
只需设置 fpprintprec:5 即可获得 5 位小数(四舍五入)。

另一种方法是读取数字,这样 3.1 实际上永远不会转换为二进制,而是最初被解析为 3+1/10。从那时起,所有理性算术都可以准确地完成。 (有理算术不包括平方根、对数、余弦……,只是 +-*/ 和整数幂)。目前这不是 Maxima 的一部分。

顺便说一句,您的 0.1+0.2 示例在 wxmaxima 的显示中显示为 0.3。但它隐藏了实际值,因为 %-3/10 实际上不是零。
玩 fpprec:50;我们可以表示 0.3,仍然是二进制,但通过输入 0.3b0 可以表示更多的数字。

0.1+0.2-0.3b0;
给出 4.440892098500626161694526672363281263363823550461b-17

哦,如果您担心有关 Maxima 转换的消息
浮点数为有理数,设置ratprint:false。它不会改变计算,只会改变警告。

Since "regular" calculators will get such questions wrong, sometimes, it is not clear what will truly solve your problem. If you are setting up an automated arithmetic test to see if people can get the right answer, then maybe what you should be testing for is agreement up to some tolerance.

(examples for your calculator can be constructed by computing variations of 1.0/3.0 and then multiplying by 3. You might get 0.9999... . Some calculators round better than others, so the examples must be slightly more subtle. Like 1.0/30.0 X 30.0)

Getting back to Maxima, you can test to see if abs(a-b)< tolerance, or perhaps
abs((a-b)/max(a,b)) < relativetolerance.

Now if what would really solve your problem is just less precision on OUTPUT, you could
just set fpprintprec:5 to get 5 decimal digits (rounded).

An alternative is to read your numbers so that 3.1 in fact never gets converted to binary, but is initially parsed as 3+1/10. From that point on, all rational arithmetic can be done exactly. (Rational arithmetic does not include square roots, logs, cosine..., just +-*/ and integer powers). This is not part of Maxima right now.

Incidentally, your example of 0.1+0.2 shows up as 0.3 in wxmaxima's display. But it is concealing the actual value, because %-3/10 is not, in fact, zero.
playing with fpprec:50; we can represent 0.3, still in binary, but to lots more digits by typing 0.3b0.

0.1+0.2-0.3b0;
gives 4.440892098500626161694526672363281263363823550461b-17

oh, in case you are concerned by the messages about Maxima converting
floats to rational numbers, set ratprint:false. It doesn't change the computing, just the warnings.

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