# The “max” function on Tikz being annoying, help?

I used Tikz to chart the following function.

This is fine, except that I want the function to equal 0 instead of negative values. So I tried using "max(0,f(x))", where f(x) is my function (specified below):

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (4,4);
\draw[
samples=100,
fixed point arithmetic,
scale=1.3,domain=0.001:4,smooth,variable=\x,blue] plot
({\x},{max(0,(\x*(0.005*\x)^0.5-0.005)/((0.33*\x+(0.005*\x)^0.5)))});
\end{tikzpicture}
\end{document}


But this code does not run. What am I doing wrong?

• Just for the records: your code does not compile on my machine. Does it run thought on yours? Which TeX installation are you using? – marmot Aug 10 '18 at 13:59
• The same issue arises with min as well. (I tried to see what's going on by comparing min and max in pgflibraryfixedpointarithmetic.code.tex, just to discover that they do not differ, and min doesn't work as well.) – marmot Aug 10 '18 at 14:50
• What is also odd is that fp does come with min and max, they are in \FPmin and \FPmax. – marmot Aug 10 '18 at 15:12
• @marmot try without max, I think it will compile. Let me know if otherwise. – Raaja Aug 10 '18 at 15:32
• @Raaja Yes, of course, without max (or min) it compiles. But that's not the issue, I think. Rather, I think pafnuti has discovered an important bug. (However, the fact that some of us can compile and others not seems to suggest that there are different versions of the fp package at work. I added some \typeouts to sort of substantiate that.) – marmot Aug 10 '18 at 15:36

Removing the fixed point arithmetic should ensure you get what you want. because, when you specify the fixed point constraint, the max(.) assumes its domain to be integers (I think). However, your function returns real-values. Hence, this will cause an internal contradiction and hence you see the errors. If you now remove the constraints, all should go as you desire. Below is the MWE based solution for that.

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{fp}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (4,4);
\draw[
samples=100,
scale=1.3,domain=0.001:4,smooth,variable=\x,blue] plot ({\x},{max(0,(\x*(0.005*\x)^0.5-0.005)/((0.33*\x+(0.005*\x)^0.5)))});
\end{tikzpicture}
\end{document}


which will give you:

• Are you sure that it assumes integers? And even if that's the case, how would that explain that max(0,...) yields something negative? – marmot Aug 10 '18 at 14:00
• @marmot if it's wrong (I sense it from your question), please correct me. What I understood from OP's question is that when OP uses no max they get negative range, but when OP uses max tje code doesn't work. By making that assumption on fixed point it worked. But I have not read that in the manual. Also, in here, I do not know how to see the output data. – Raaja Aug 10 '18 at 14:33
• I have not much experience with that library but I would be really surprised if it assumed integers. And, even if it did, it would not explain the fact that max(0,...) can become negative, would it? In addition, there is is @Bamboo's answer, which is at odds with that assumption. – marmot Aug 10 '18 at 14:40
• @marmot I'm in agreement with you. OP tried wo max and gets the negative, but when tried with max it results in error. – Raaja Aug 10 '18 at 15:30

Using an ifthenelse conditional, the result is good with or without fixed point arithmetic. I guess this supports @marmot interrogations regarding the max function.

As proposed by @marmot, I also included a redefinition of the max function as Max(x,y) based on ifthenelse to avoid the repetition of the tested function.

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\begin{document}
\tikzset{declare function={Max(\X,\Y)=ifthenelse(\X>\Y,\X,\Y);}}
\begin{tikzpicture}
\draw (0,0) rectangle (4,4);
\draw[
samples=100,
fixed point arithmetic,
scale=1.3,domain=0.001:4,
smooth,
variable=\x,blue] plot
({\x},{Max(((\x*(0.005*\x)^0.5-0.005)/((0.33*\x+(0.005*\x)^0.5))),
0)});
\end{tikzpicture}
\end{document}


• Thanks! I'm wondering if you could the same for min? ;-) – marmot Aug 10 '18 at 16:13
• @marmot What do you mean ? – BambOo Aug 11 '18 at 10:39