所以我試著用 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}