# Why does the samples option conflict with fp?

Minimal working example:

\documentclass{amsart}
\usepackage{fp}
\usepackage{tikz}
\usetikzlibrary{fixedpointarithmetic}
\begin{document}
\begin{tikzpicture}[scale=2, fixed point arithmetic]
\draw[domain=0:360, samples=100] plot ({2*cos(\x)}, {sin(\x)});
\end{tikzpicture}
\end{document}


This renders with error: ! FP error: Illegal character 100 found in float number!.

If I remove the samples option or the fixed point arithmetic option, it renders without error. I haven't been able to find any documentation to suggest why this is or how the problem can be avoided.

Using the smooth option without manual sample control gives something that does not suffice: not only does the curve look slightly lumpy, the smoothing fails completely at the endpoints of the parameterisation.

There is some success if the endpoints of the parameterisation are moved to the point of lowest curvature (by changing the domain to 90:450), but there is still a noticeable sharpness, and this does not affect the slight lumpiness.

Breaking the render up into two piece like such, to double the samples:

\draw[domain=90:270, smooth] plot ({2*cos(\x)}, {sin(\x)});
\draw[domain=270:450, smooth] plot ({2*cos(\x)}, {sin(\x)});


You get two halves of an ellipse that are both smoother than the original render, but quite inexplicably, at distance from one another.

NB: I understand the native draw ellipse capability. The example above is highly simplified from my actual needs.

Edit: Using a bug fix found in another question:

replacing

\usetikzlibrary{fixedpointarithmetic}


with

\endlinechar=-1
\usetikzlibrary{fixedpointarithmetic}
\endlinechar=13


The error with half of the ellipse being displaced is corrected; the result is a fair but not ideal workaround. The rest of the question still stands.

The problem is in the fixedpointarithmetic that assumes \pgfmathmax@ expects two arguments. Instead, a construction such as \pgfmathparse{max(1,2,3)} calls

\pgfmathmax@{{1}{2}{3}}


and it's the duty of \pgfmathmax@ to sort out the thing and find the maximum.

Since samples=<value> does max(2,<value>) the problem arises.

The fixedpointarithmetic library does

\let\pgfmathmax@\pgfmathfpmax@


where the macro \pgfmathfpmax@ is defined with two arguments. Thus every time \pgfmathmax@ is called, it consumes one token more. One can easily fix the bug if max is only computed between two numbers, but if a TikZ program wants max between more than two quantities, it will break.

For instance

\begin{tikzpicture}[fixed point arithmetic]
\pgfmathparse{max(2,100,123)};
\end{tikzpicture}


will result in storing 100 in \pgfmathresult and printing 123.

Calls of the function min will break in the same way.

Also \pgfmathfpmax is buggy: after \pgrmathfpmax{1}{2}, the macro \pgfmathresult will expand to \pgfmathresult, not to 2. The same for \pgfmathfpmin.

A very partial fix for the particular problem is

\documentclass{amsart}
\usepackage{etoolbox}
\usepackage{fp}
\usepackage{tikz}

\usetikzlibrary{fixedpointarithmetic}

\makeatletter
% partial fix for the bug
\let\buggy@pgfmathfpmax@\pgfmathfpmax@
\def\pgfmathfpmax@#1{\buggy@pgfmathfpmax@#1}
\patchcmd\pgfmathfpscientific{\unskip}{}{}{}
\patchcmd{\pgfmathfppow@}{\unskip}{}{}{}
% remove the wrong space in \FP@pow
\patchcmd{\FP@pow}
{ }{}
{\typeout{Fixed \string\FP@pow}}
{\typeout{\noexpand\FP@pow needs no patch}}
\makeatother

\begin{document}

\begin{tikzpicture}[scale=2, fixed point arithmetic]
\draw[domain=0:360, samples=99] plot ({2*cos(\x)}, {sin(\x)});
\end{tikzpicture}
\end{document}


However, this will only fix the particular call done when the samples key is processed.

My advice is to forget about the fixedpointarithmetic library until the bugs are fixed.

I added also a couple of safe fixes, for avoiding the creeping in of spaces, better than loading the library with \endlinechar=-1.

The max code picks up a trailing \fi a full fix would be to track down exactly where that hasn't been removed properly but a quick fix is to test for it and handle it in max:

\documentclass{amsart}
\usepackage{fp}
\usepackage{tikz}
\endlinechar=-1
\usetikzlibrary{fixedpointarithmetic}
\endlinechar=13

\makeatletter
\def\pgf@fitest{\fi}

\def\pgfmathfpmax@#1#2{%
\def\pgf@tmp{#2}%
\ifx\pgf@tmp\pgf@fitest
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\pgf@fitest\pgfmathfpmax@#1}%
{\begingroup%
\FPifgt{#1}{#2}%
\def\pgfmathresult{#1}%
\else%
\def\pgfmathresult{#2}%
\fi%
\pgfmath@smuggleone\pgfmathresult%
\endgroup%
}}
\let\pgfmathmax@=\pgfmathfpmax@%

\begin{document}
\begin{tikzpicture}[scale=2, fixed point arithmetic]
\draw[domain=0:360, samples=100] plot ({2*cos(\x)}, {sin(\x)});
\end{tikzpicture}
\end{document}