I think I can provide some more insight into the questions here.
The good news at first: providing \begin{axis}[use fpu=false]
will enable you to use all custom math functions (as long as they work in pgf, I guess).
Now the details:
Let me summarize the state of the discussion:
When we use custom math functions in pgfplots, we have
- the solution containing `\pgf@x=#1pt` leads to
! Illegal unit of measure (pt inserted).
Y
l.24 \addplot[domain=-5:5, samples=50]{double(x)};
- The solution with `\pgfmathmultiply{#1}{#1}` worked
- The solution(s) with `\pgfmathparse` worked as well
- The solution with `\pgfmathand{\pgfmathless{#1}{1}}{\pgfmathgreater{#1}{0}}` failed (in my case something strange with `! Extra else`
The problem is caused by the fact that PGFPlots has the initial configuration 'use fpu=true'. The fpu is a PGF library; it replaces the math module by something like single precision floating points.
As long as user contributed math functions rely only on high-level math functions (like \pgfmathparse
or \pgfmathmultiply
as above), the codebase will transparently use the FPU - and everything is consistent.
But as soon as you employ TeX registers, things become different: Writing \pgf@x=#1pt
means that the first argument is interpreted as a fixed point number in the range -16000...16000 (roughly). With use fpu=true
, both is essentially violated: the fpu has neither fixed point numbers nor is it restricted to this data range. The error message arises because (at the point of this writing), floating point numbers are stored like 1Y1.0e1
where the Y
separates "flags" from mantissa. The Y
is the first character where \pgf@x=#1pt
bails out. Note, however, that the FPU is smart enough to detect if the RETURN VALUE of a custom function is a TeX register number or a float. But I am unaware of any way of asking "will the function handle floats?".
So, as already mentioned, use fpu=false
disables the FPU; pgfplots will then operate with fixed point numbers in the range -16000...16000 and all the math functions should work.
The other solution is to use the basic layer math functions like \pgfmathmultiply
. Note that unless I am mistaken, \pgfmathmultiply
will also invoke \pgfmathparse
(which is expensive). Use \pgfmathmultiply@
to suppress argument parsing (which, however, needs a \makeatletter
before defining the custom function).
Unfortunately, I am unaware of why the \pgfmathand{} {}
solution fails. Does it work in PGF?
I hope this helps here.