因此,我尝试使用 LaTeX 进行一些复杂的计算,但它总是给出无意义的答案。我尝试计算在给定某些条件下可以覆盖球的层数,但 LaTeX 总是给出否定答案!经过几个小时的折腾,我终于找到了错误,如下所示的 MWE
\documentclass[border=1mm]{article}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\usepackage{pgfplots}
\begin{document}
\pgfmathsetmacro{\earthRadiusKm}{6371}
\pgfmathsetmacro{\coinRadiusM}{1.05 / 1000}
\pgfmathsetmacro{\coinHeightM}{1.7 / 1000}
\pgfkeys{/pgf/fpu, /pgf/fpu/output format=fixed}
\pgfmathsetmacro{\coinsTotalHeight}{3.27*10^17}
\pgfmathsetmacro{\earthRadiusM}{6371*1000}
\pgfmathsetmacro{\radiusCoinsLayerCubedMtest}{%
(\earthRadiusM^3)^(1/3) - \earthRadiusM}
\pgfmathsetmacro{\R}{
((\earthRadiusM)^3 + 1.5 * (\coinRadiusM) * (\coinsTotalHeight))^(1/3)
}
\pgfmathsetmacro{\layers}{
(\R - \earthRadiusM)/(\coinHeightM)
}
\pgfkeys{/pgf/fpu=false}
$\sqrt{(R_\oplus^3)^{1/3} - R_\oplus}$ equals $0$ not \radiusCoinsLayerCubedMtest !
The radius is
\begin{align*}
R = \sqrt[3]{R_\oplus^3 + \frac{3}{2}r_m h_c}
\approx
\R
\end{align*}
%
Which means that the total number of layers are
%
\begin{align*}
n &= \frac{R - R_\oplus}{h_m} \\
&\approx \frac{\R - \earthRadiusM}{\coinHeightM}
\approx \layers
\end{align*}
\end{document}
问题是
(something^3)^(1/3) - something
不等于零,大概是因为舍入误差。很明显,上面的表达式应该为零,但事实并非如此。相反,我得到的-1400.0
是完全无稽之谈。我怎样才能让 fpu 库也准确地计算平方根?
我的实际例子稍微复杂一些,但归根结底就是计算同样的事情。
答案1
答案2
使用fp
模块expl3
以及一些变量的语法糖,也可以确保我们不会重新定义现有的命令。
然而,你不能指望(X3)1/3 =X。
\documentclass{article}
\usepackage{mathtools,xfp}
\ExplSyntaxOn
\NewDocumentCommand{\setfpvar}{mm}
{
\fp_zero_new:c { nebu_var_#1_fp }
\fp_set:cn { nebu_var_#1_fp } { #2 }
}
\NewExpandableDocumentCommand{\fpvar}{m}
{
\fp_use:c { nebu_var_#1_fp }
}
\ExplSyntaxOff
\begin{document}
\setfpvar{earthRadiusKm}{6371}
\setfpvar{coinRadiusM}{1.05 / 1000}
\setfpvar{coinHeightM}{1.7 / 1000}
\setfpvar{coinsTotalHeight}{3.27*10^17}
\setfpvar{earthRadiusM}{6371*1000}
\setfpvar{radiusCoinsLayerCubedMtest}{
(\fpvar{earthRadiusM}^3)^(1/3) - \fpvar{earthRadiusM}
}
\setfpvar{R}{
((\fpvar{earthRadiusM})^3 + 1.5 * (\fpvar{coinRadiusM}) * (\fpvar{coinsTotalHeight}))^(1/3)
}
\setfpvar{layers}{
(\fpvar{R} - \fpvar{earthRadiusM})/(\fpvar{coinHeightM})
}
$\sqrt{(R_\oplus^3)^{1/3} - R_\oplus}$ equals
$\fpvar{radiusCoinsLayerCubedMtest}$
\bigskip
The radius is
\begin{align*}
R = \sqrt[3]{R_\oplus^3 + \frac{3}{2}r_m h_c}
\approx
\fpvar{R}
\end{align*}
which means that the total number of layers is
\begin{align*}
n &= \frac{R - R_\oplus}{h_m} \\
&\approx \frac{\fpvar{R} - \fpvar{earthRadiusM}}{\fpvar{coinHeightM}}
\approx \fpvar{layers}
\end{align*}
\end{document}