2

I understand that the less automatic solution to compare multiple integer logical statements is by using \int_compare_p:nNn {...}{...}{...} along with \bool_if:nTF {condition(s)}{true}{false}. My question is why does \fp_compare:nTF {...}{...}{...} works fine with logical operators but \int_compare:nTF {...}{...}{...} yield an error even though all of the values used are integers?

\documentclass{article}

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

\NewDocumentCommand{\compareHardcode}{mm}{
  \bool_if:nTF {
    \int_compare_p:nNn {1} < {2} && \int_compare_p:nNn {2} < {3}
  }{#1}{#2}
}

\NewDocumentCommand{\compareError}{mmm}{
  %will not work using \int_compare:nTF 
  \int_compare:nTF {#1}{#2}{#3}
}
\ExplSyntaxOff
\begin{document}
  \compare{1 < 2 && 2 < 3}{yes}{no} % prints yes
  \compareHardcode{yes}{no}         % prints yes
  % Erroneous line commented:
  % \compareError{1 < 2 && 2 < 3}{yes}{no}
\end{document}
9
  • 1
    Because it is documented behaviour... Use \int_compare:nTF { 1 < 2 < 3} {yes} {no} instead.
    – Skillmon
    Aug 16 at 20:15
  • 1
    @Skillmon, thank you for the suggestion. I wonder why this is the case? Aug 16 at 20:19
  • The only reason I can tell is: Because it was coded and documented that way. Maybe a kernel-team member knows the reason behind this decision...
    – Skillmon
    Aug 16 at 20:21
  • 1
    @M.AlJumaily the underling \ifnum and l3fp tests are very different, so there are limits on how far you can make things look the same, but I don't recall this difference being specifically discussed, Aug 16 at 20:23
  • 1
    @M.AlJumaily typical languages provide floating point. tex does not. l3's int expressions are a thin wrapper around the primitive \numexpr and do not have infix conditional expressions. You can combine integer comparisons using l3boolean functions, just not as inline int expressions. l3fp is different as it is not based on any tex provided expression syntax. Aug 22 at 0:27

1 Answer 1

3

Something like 2<3 is a valid ⟨fp expr⟩ and \fp_eval:n { 2<3 } returns 1. You can also use logical operators. Examples:

\documentclass{article}

\begin{document}

\fpeval{1<2}

\fpeval{1<2 && 2<3}

\fpeval{1<2 || 3<2}

\fpeval{1>2 || 3<2}

\end{document}

This prints

1
1
1
0

So \fp_compare:nTF { 1<2 && 2<3 } { yes } { no } returns “yes”, because the evaluation is 1, which is nonzero.

To the contrary, 1<2 is not a valid ⟨int expr⟩. Moreover, you cannot use \int_compare:nTF { 1<2 && 2<3 } { yes } { no }, because the argument is invalid: the argument has to be in the form

⟨int expr⟩⟨relation⟩⟨int expr⟩⟨relation⟩…⟨int expr⟩⟨relation⟩⟨int expr⟩

and logical operators are not allowed.

3
  • Could you say where \fpeval is documented? (I tried the clsguide, xparse and (not very hopefully) interface3.)
    – cfr
    Aug 19 at 3:05
  • Thank you for the clarification. I wonder why fp logic works as expected (even though there might be issues with rounding) but integer logic doesn't work? If anything, I would imagine that fp logic would be problematic rather than integer logic. Aug 21 at 23:02
  • @M.AlJumaily Integer expressions are bound by the implementation of \numexpr in e-TeX; to the contrary, fp-expressions are completely implemented in the kernel (and are much slower).
    – egreg
    Aug 22 at 7:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .