# New command with if/then statement conditioned on non-integer number?

In LaTeX, how do I declare a new command that gives different output depending no the result of a test with non-integer numbers? Specifically, I'd like to a command to compare a number with 0.01 (the numbers would come from a different script): if the number is greater than or equal to 0.01, print p = 0.45 (or whatever number p is); if the number if less than 0.01, print p < 0.01.

I have looked at ifnumless in etoolbox but it only takes integers and dimensions.

How do I do this with numbers with decimal points? Thanks.

-
Maybe calc can help. –  Martin Dec 13 '13 at 4:57
The typical (elementary) approach would be to use lengths. That is, translate 0.05 to 0.05pt and then perform a length test (like \ifdim 0.05pt<0.01pt\relax<true>\else<false>\fi). etoolbox does provide length tests, and so does other packages. The following seems to be a duplicate: Conditional using non-integer numbers - it handles the same approach of real-to-length usage. –  Werner Dec 13 '13 at 5:55

You were almost there... If you want to compare real numbers you have to compare dimensions, so use \ifdimless instead of \ifnumless like in the following example:

\documentclass{article}
\usepackage{etoolbox}

\newcommand{\checknum}[1]{%
\ifdimless{#1pt}{.01pt}{$p < 0.01$}{$p = #1$}%
}

\begin{document}

\checknum{0.45}

\checknum{0.01}

\checknum{0.005}

\end{document}


-
oh... so close... thanks! –  ceiling cat Dec 13 '13 at 6:35

You can use the l3fp module of LaTeX3:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\fpcompare}{mmm}
{
\fp_compare:nTF { #1 } { #2 } { #3 }
}
\ExplSyntaxOff

\begin{document}

\newcommand{\checknum}[1]{%
\fpcompare{#1 < .01}{$p < 0.01$}{$p = #1$}%
}

\checknum{0.45}

\checknum{0.01}

\checknum{0.005}

\renewcommand{\checknum}[1]{%
\fpcompare{#1 >= .01}{$p = #1$}{$p < 0.01$}%
}

\checknum{0.45}

\checknum{0.01}

\checknum{0.005}

\end{document}


I gave the example twice to show that the comparison operator can be <, =, >, but also <= and >=. The result is the same because the two final arguments have been swapped.

-