Direct evaluation of fp expression
Not sure if this approach has any gotcha's.
\documentclass{article}
\usepackage[nomessages]{fp}
\newcommand\FPuse[1]{\FPeval{\result}{#1}{\result}}
\begin{document}
$\cos(\pi)=\FPuse{clip(cos(pi))}$\
$\sin(\pi/3)=\FPuse{sin(pi/3)}$
$\sin(\pi/3)=\FPuse{round(sin(pi/3),3)}$
\end{document}
In the comments below, jfbu and I discuss why I grouped {\result}
at the end of the \FPuse
definition. First, let's see what happens if I ungroup it:
\newcommand\FPuse[1]{\FPeval{\result}{#1}\result}
The result on the first operation is
What we see is that \result
sets itself as {} - 1
, using a binary minus sign. The conclusion is that \FPeval{}{}
creates a \bgroup...\egroup
quantity that, in math mode, causes the subsequent minus sign to act in a binary fashion. Thus, the only way to eliminate this problem (without changing the fp
package), is to isolate the final \result
in its own group, as I did in my original code.
While jfbu has probed a bit into the guts of fp
, I am no expert to know if the fp
code can be revised to use \begingroup...\endgroup
instead (which is truly transparent in math mode) or not. I do know that fp
has a few issues, for example, a stray space is introduced via \FPpow
which has to be \unskip
ed after its use.
See jfbu's comments below for more information on the group issue.
Not with fp
. It is possible with expl3
, though.
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
% make an internal function available to the user
\cs_set_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff
\begin{document}
$\cos(\pi)=\fpeval{cos(pi)}$
$\sin(\pi/3)=\fpeval{sin(pi/3)}$
$\sin(\pi/3)=\fpeval{round(sin(pi/3),3)}$
\end{document}