Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ifthenelse evaluates both branches #1006

Open
hmenke opened this issue Apr 14, 2021 · 4 comments
Open

ifthenelse evaluates both branches #1006

hmenke opened this issue Apr 14, 2021 · 4 comments
Labels

Comments

@hmenke
Copy link
Member

hmenke commented Apr 14, 2021

Brief outline of the bug

ifthenelse always evaluates both branches, regardless of the condition. It should short-circuit and only evaluate the branch that is actually taken.

Minimal working example (MWE)

\documentclass{article}
\usepackage{pgfmath}
\begin{document}
\pgfmathprint{ifthenelse(1 == 1, 1, 1/0)}
\pgfmathprint{ifthenelse(0 == 1, 1/0, 1)}
\end{document}
@muzimuzhi
Copy link
Member

It seems some laziness support is needed, either internal to ifthenelse and ?, or public as, for example \pgfmathdeclarelazy(function|operator).

@ilayn
Copy link
Member

ilayn commented Apr 15, 2021

Also related questions in TeX.SX

@hmenke
Copy link
Member Author

hmenke commented Apr 16, 2021

The culprit lies in the fact that the definition of \pgfmathifthenelse is

\pgfmathifthenelse:
macro:#1#2#3->\pgfmathparse {#1}\let \pgfmath@argument@1 =\pgfmathresult \pgfmathparse {#2}\let \pgfmath@argument@2 =\pgfmathresult \pgfmathparse {#3}\let \pgfmath@argument@3 =\pgfmathresult \pgfmathifthenelse@ {\pgfmath@argument@1 }{\pgfmath@argument@2 }{\pgfmath@argument@3 }

So we either need some separate implementations for all functions to handle laziness or we just redefine \pgfmathifthenelse after it has been defined using \pgfmathdeclarefunction. Are there any other functions that have to evaluate their arguments lazily?

@muzimuzhi
Copy link
Member

Are there any other functions that have to evaluate their arguments lazily?

and and or

\pgfmathprint{and(false, 1/0)}
\pgfmathprint{or(true, 1/0)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants