开发者

Compare floating point numbers in Latex

开发者 https://www.devze.com 2022-12-27 18:30 出处:网络
I am trying to use \\ifthenelse to do a floating point comparison. This is the pgf/tikz code, which works if \\y is integer only, but not otherwise:

I am trying to use \ifthenelse to do a floating point comparison. This is the pgf/tikz code, which works if \y is integer only, but not otherwise:

\foreach \y in {3,3.5,...,6}开发者_C百科 {
    ifthenelse{\y<3}{
        ...
    }{
        ...
    }
}


You can not use floating variables. Use dimens instead of. For example

\newdimen \y 
\y = 3.2pt
\ifdim \y < 3.45pt ... \else ... \fi


To expand on Alexey's suggestion of using dimensions, here is some working TikZ code that I think will solve your problem:

\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}
\begin{document}
  \begin{tikzpicture}
    \foreach \y in {3,3.5,...,6} {
      \ifthenelse{\lengthtest{\y pt > 4.5pt}}{
        \node at (0,\y) {\y\ is greater than 4.5!};
      }{
        \node at (0,\y) {\y\ is less than 4.5};
      }
    }
  \end{tikzpicture}
\end{document}


If you already have defined some float you can use the following trick which worked for me (based on Alexeys post):

    \def\someFloat{1.5}

    % prepare comparison by building a dummy dim value
    \newdimen\dummyDim
    \dummyDim = \someFloat pt

    % compare:
    \ifdim \dummyDim > 0pt %
       % ...
    \else
       % ...
    \fi
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号