maxima CAS:替换了浮动鼠
在 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 ofis(1.4*28=39.2);
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自浮点指南:
在您的情况下,1.4 和 39.2 都不能完全表示为二进制分数,并且计算结果最终的舍入方式与文字 39.2 不同。
如果您想避免此类问题,则必须避免使用二进制浮点数。我认为在 Maxima 中,通过使用真分数最容易做到这一点:
应该有效
From The Floating-Point Guide:
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:
should work
由于“常规”计算器会出错,因此有时并不清楚什么才能真正解决您的问题。如果您正在设置自动算术测试来查看人们是否可以获得正确的答案,那么也许您应该测试的是在一定容忍度范围内的一致性。
(计算器的示例可以通过计算 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.