如何将一个数字除以另一个数字以得出实数结果(而不是整数)?

如何将一个数字除以另一个数字以得出实数结果(而不是整数)?

如何basiceval编写一个类似的函数来给出实数结果而不是整数,从而使这里的第三个计算更准确?

\documentclass[american, a4paper]{report}

\def\basiceval#1{\the\numexpr#1\relax}

\begin{document}

\def\variableA{30}
\def\variableB{10}
\def\variableC{3}

\begin{itemize}
\item \basiceval{360/\variableB}
\item \basiceval{\variableA/\variableB}
\item \basiceval{\variableB/\variableC}
\end{itemize}

\end{document}

答案1

如果您选择使用 LuaLaTeX 而不是 pdfLaTeX,那么您可以按如下方式定义宏:

\def\basiceval#1{\directlua{tex.sprint(#1)}}

以下是修改后的 MWE 的结果:

在此处输入图片描述

% !TEX TS-program = lualatex
\documentclass[american, a4paper]{report}

\def\basiceval#1{\directlua{tex.sprint(#1)}}

\def\variableA{30}
\def\variableB{10}
\def\variableC{3}

\begin{document}

\begin{itemize}
\item \basiceval{360/\variableB}
\item \basiceval{\variableA/\variableB}
\item \basiceval{\variableB/\variableC}
\end{itemize}

\end{document}

答案2

\documentclass[american, a4paper]{report}

\usepackage{fp}
\def\basiceval#1{\the\numexpr#1\relax}

\begin{document}

\def\variableA{30}
\def\variableB{10}
\def\variableC{3}

\begin{itemize}
\item \basiceval{360/\variableB}
\item \basiceval{\variableA/\variableB}
\item \basiceval{\variableB/\variableC}
\end{itemize}

\FPupn{va}{30}
\FPupn{vb}{10}
\FPupn{vc}{3}

\FPupn{varesult}{360 vb swap / clip}
\FPupn{vbresult}{va vb swap / clip}
\FPupn{vcresult}{vb vc swap / 8 round}

\begin{itemize}
\item \varesult
\item \vbresult
\item \vcresult
\end{itemize}


\end{document}

答案3

\documentclass{article}
\usepackage{fp}
\newcommand\basiceval[1]{\basicevalhelp#1\relax}
\def\basicevalhelp#1/#2\relax{\FPdiv\result{#1}{#2}\result}
\begin{document}
\def\variableA{30}
\def\variableB{10}
\def\variableC{3}

\begin{itemize}
\item \basiceval{360/\variableB}
\item \basiceval{\variableA/\variableB}
\item \basiceval{\variableB/\variableC}
\end{itemize}
\end{document}

在此处输入图片描述

相关内容