FP 包:负数幂的错误

FP 包:负数幂的错误

我正在使用 FP 包进行 AMC(自动多项选择)的多项选择考试。对于随机化,我使用了较长的公式,有时会导致错误消息。出现这些错误消息的原因是 FP 包似乎在负数的幂方面存在问题。无论使用例如 (-5)^2 还是 pow(2, -5),这总是会导致消息“FP 错误:负值的对数!”或“FP 错误:UPN 堆栈为空!”。当然结果是错误的。

令人惊讶的是,我没有找到任何关于这种错误行为的帖子。我不敢相信我是第一个遇到这些问题的人。有什么办法可以解决这个问题吗?附件中有一个最小文档和相应的输出。

\documentclass[10pt]{article}
\usepackage{fp}
\begin{document}
\setlength{\parindent}{0pt}
\FPeval\Result{5^2}
Result of 5\textasciicircum 2: \Result{} (correct)

\FPeval\Result{(-5)^2}
Result of (-5)\textasciicircum 2: \Result{} (wrong)

\FPeval\Result{abs(-5)^2}
Result of abs(-5)\textasciicircum 2: \Result{} (correct)

\FPeval\Result{pow(2,5)}
Result of pow(2, 5): \Result{} (correct)

\FPeval\Result{pow(2,-5)}
Result of pow(2, -5): \Result{} (wrong)

\FPeval\Result{pow(2,abs(-5))}
Result of pow(2, abs(-5)): \Result{} (correct)

\FPeval\Result{pow(-2,5)}
Result of pow(-2, 5): \Result{} (correct)

\FPeval\Result{pow(-2,-5)}
Result of pow(-2, -5): \Result{} (wrong)

\FPset\myvar{-5}
\FPeval\Result{pow(2,myvar)}
Result of \verb+\FPset\myvar{-5}+ and pow(2, myvar): \Result{} (wrong)
\end{document}

tex 文件产生以下输出(FP 0.8 / texlive 2021 / Linux Mint 21.1): 在此处输入图片描述

答案1

我相信现在是时候过渡到 LaTeX3/l3fp 了,它在最新的发行版中可用,不需要任何软件包。

^**可以使用来代替。pow未提供函数。

代码

\documentclass[10pt]{article}
\NewDocumentCommand{\FPeval}{mm}{\edef#1{\fpeval{#2}}}
\begin{document}
\setlength{\parindent}{0pt}
\FPeval\Result{5^2}
Result of \verb|5^2|: \Result\ (correct)

\FPeval\Result{(-5)^2}
Result of \verb|(-5)^2|: \Result\ (\textbf{correct})

\FPeval\Result{abs(-5)^2}
Result of \verb|abs(-5)^2|: \Result\ (correct)

\FPeval\Result{(-5)^(-2)}
Result of \verb|(-5)^(-2)|: \Result\ (\textbf{correct})

\FPeval\Result{5^(-2)}
Result of \verb|5^(-2)|: \Result\ (correct)
\end{document}

输出

结果5^2:25(正确)
结果(-5)^2:25(正确的)
结果abs(-5)^2: 25 (正确)
结果(-5)^(-2): 0.04 (正确的)
结果5^(-2):0.04(正确)

答案2

当人们遇到限制时,他们就会意识到它们。对于几乎不学数学的人来说,这不是问题。你说,“我开始使用它fp是因为 AMC 建议使用它,而没有花几分钟时间寻找最新的解决方案”。除了fpxfp还建议使用 LaTeX3/l3fp。你还应该知道 lua 的计算方法。一篇帖子这里讨论了各种软件包中出现的一些问题。我建议使用计算机代数系统 (CAS),即使您已经接受了答案,因为我想确保您知道最新的解决方案。该帖子中提到的 SAGE 是一个开源 CAS,与 Mathematica 类似,但免费。它的网站是这里。 这鼠尾草包让您可以访问 Sage 和 Python。正如您所说,“我用 Python 开发了一个过滤器,可以将所有测验问题从 Moodle 转换为 AMC,包括现在依赖 fp 的所有公式。”访问 Python 是您可以在 LaTeX 文档中享受的额外好处。这是用于sagetex解决您的问题的 LaTeX 文档。

\documentclass[10pt]{article}
\usepackage{sagetex, amsmath}
\begin{document}
\setlength{\parindent}{0pt}
Result of 5\textasciicircum 2: $\sage{5^2}$

Result of (-5)\textasciicircum 2: $\sage{(-5)^2}$

Result of abs(-5)\textasciicircum 2: $\sage{abs(-5)^2}$

Result of pow(2, 5): $\sage{pow(2,5)}$

Result of pow(2, -5): $\sage{pow(2,-5)}$ 

Result of pow(2, abs(-5)): $\sage{pow(2, abs(-5))}$

Result of pow(-2, 5): $\sage{pow(-2,5)}$

Result of pow(-2, -5): $\sage{pow(-2,-5)} \approx \sage{pow(-2,-5).n(digits=3)} $
\end{document}

Cocalc 中运行的输出如下: 在此处输入图片描述

需要注意的几点:首先,无需设置结果,只需使用 即可获得答案\sage{}。这样可以减少输入。其次,请注意 会sage以分数形式给出精确的答案,这是精确的,而不是小数。最后,请注意,我可以使用 强制 SAGE 给出小数\sage{pow(-2,-5).n(digits=3)};digits=3 表示精确到 3 位。您没有注意到的是,SAGE 是 CAS,可以处理各种问题(积分、导数、矩阵、图论等)而无需重新发明轮子。我对绘制 Weierstrass 函数的回答这里需要用 Python 编程进行计算,然后强制执行tikz以创建图表。搜索此网站可以找到sagetex许多其他使用示例。

鉴于你使用了一些数学知识并且熟悉 Python,你应该看看sagetex跳转到 LaTeX3/l3fp 之前先了解一下。Python、CAS 和 LaTeX 是一个非常强大的组合。SAGE 的缺点是它不是您的 LaTeX 发行版的一部分。因此,它需要安装在您的计算机上,或者您可以注册一个免费的可钙帐户。

相关内容