比较计算出的实数

比较计算出的实数

“\ifdimless” 可以比较两个实数。但是,我们如何将计算结果提供给此函数?

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{etoolbox} 

\begin{document}

% This comparison works well: small versus specified 
\newlength{\SmallLength}
\setlength{\SmallLength}{1.2 pt}
\newlength{\SpecifiedLength}
\setlength{\SpecifiedLength}{3.4 pt}
\ifdimless{\SpecifiedLength}{\SmallLength}{specified smaller}{specified higher}%

% Second is divided by First, and the result works well (\DividedNumbers)
\def\FirstNumber{2.3}
\def\SecondNumber{4.5}
\def\DividedNumbers{\pgfmathparse{div(\SecondNumber,\FirstNumber)}\pgfmathresult}
\DividedNumbers

%\Divided is taken from \DividedNumbers: doesn't work...
\newlength{\DividedLength}
%\setlength{\DividedLength}{\DividedNumbers pt}
%\ifdimless{\DividedLength}{\SmallLength}{divided smaller}{divided higher}%

\end{document}

答案1

存储结果而不是计算:

指定较高
1
除以较小

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{etoolbox} 

\begin{document}

% This comparison works well: small versus specified 
\newlength{\SmallLength}
\setlength{\SmallLength}{1.2 pt}
\newlength{\SpecifiedLength}
\setlength{\SpecifiedLength}{3.4 pt}
\ifdimless{\SpecifiedLength}{\SmallLength}{specified smaller}{specified higher}%

% Second is divided by First, and the result works well (\DividedNumbers)
\def\FirstNumber{2.3}
\def\SecondNumber{4.5}
\pgfmathparse{div(\SecondNumber,\FirstNumber)}
\edef\DividedNumbers{\pgfmathresult}% Store result in \DividedNumbers
\DividedNumbers

% \Divided is taken from \DividedNumbers:
\newlength{\DividedLength}
\setlength{\DividedLength}{\DividedNumbers pt}
\ifdimless{\DividedLength}{\SmallLength}{divided smaller}{divided higher}%

\end{document}

答案2

我建议你使用一种更强大的方法,可以直接输入计算。interface3.pdf的手册将告诉你的参数中的“维度表达式”和的参数中的“浮点表达式”expl3的语法。\xifdimless\eval

\documentclass[]{article}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\xifdimless}{mmmm}
 {
  \dim_compare:nTF { #1 < #2 } { #3 } { #4 }
 }
\DeclareExpandableDocumentCommand{\eval}{m}
 {
  \fp_eval:n { #1 }
 }
\ExplSyntaxOff

\begin{document}

% This comparison works well: small versus specified 
\newlength{\SmallLength}
\setlength{\SmallLength}{1.2pt}
\newlength{\SpecifiedLength}
\setlength{\SpecifiedLength}{3.4pt}

% first test A
\xifdimless{\SpecifiedLength}{\SmallLength}{specified smaller}{specified higher}

% first test B
\xifdimless{1.2pt+2.2pt}{4pt-2.8pt}{specified smaller}{specified higher}

% second test
\xifdimless{\SmallLength}{\SpecifiedLength}{specified smaller}{specified higher}

% third test
\xifdimless{ \eval{4.5/2.3} pt }{\SmallLength}{divided smaller}{divided higher}

% fourth test
\xifdimless{ \eval{2.3/4.5} pt }{\SmallLength}{divided smaller}{divided higher}

\end{document}

在此处输入图片描述

答案3

正如@cfr 在评论中所建议的,您可以使用tikzmath它来实现这一点:

\documentclass[preview, border=7mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}

\begin{document}

\newlength{\SmallLength}
\setlength{\SmallLength}{1.2 pt}

\tikzmath{
  if 4.5/2.3 < \SmallLength  then {
    {divided smaller};
  } else {
    {divided higher or equal};
  };
}

\end{document}

在此处输入图片描述

相关内容