How to write Quadratic equation with negative coefficient

How to write Quadratic equation with negative coefficient in fp

For a=1, b=-5, c= 6, $\ca x^2+\cb x + \cc=0$ gives 1x^2 + -5x + 6

But i want to have x^2 -5x + 6

\documentclass{beamer}
\usepackage{fp}
\begin{document}
\FPset\ca{1}
\FPset\cb{-5}
\FPset\cc{6}
\FPqsolve\xone\xtwo\ca\cb\cc
\FPeval\xone{clip(round(xone:4))}
\FPeval\xtwo{clip(round(xtwo:4))}
Quadratic equation : $\ca x^2+\cb x + \cc=0$\\[1cm]
Result: $x = \xone \quad \text{and} \quad x = \xtwo$
\end{frame}
\end{document}


Some comparison are necessary. This assumes the coefficients are integers.

\documentclass{beamer}
\usepackage{fp}

\FPset\ca{#2}%
\FPset\cb{#3}%
\FPset\cc{#4}%
\FPqsolve\xone\xtwo\ca\cb\cc
\FPeval\xone{clip(round(xone:4))}%
\FPeval\xtwo{clip(round(xtwo:4))}%
Quadratic equation: $\ifnum\ca=1 \else \ifnum\ca=-1 -% \else \ca \fi \fi #1^2% \ifnum\cb=0 \else \ifnum\cb>0 +% \ifnum\cb=1 \else \cb \fi \else \ifnum\cb=-1 -% \else \cb \fi \fi #1% \fi \ifnum\cc=0 \else \ifnum\cc>0 + \fi \cc \fi$\\[\bigskipamount]
Result: $#1=\xone$ and $#1=\xtwo$%
}

\begin{document}

\bigskip

\bigskip

\end{frame}
\end{document}


With expl3:

\documentclass{beamer}
\usepackage{xparse}

\ExplSyntaxOn

{
Quadratic~equation:~$\str_case:nnF { #2 } { {1}{} {-1}{-} } {#2} #1^{2} \str_case:nnF { #3 } { {0}{} {1}{+#1} {-1}{-#1} } { \fp_compare:nT { #3>0 } { + } #3#1 } \fp_compare:nF { #4 = 0 } { \fp_compare:nT { #4 > 0 } { + } } #4$\\[\bigskipamount]
Result:~$#1=\sandu_solve:nnnn{+}{#2}{#3}{#4}$~and~
$#1=\sandu_solve:nnnn{-}{#2}{#3}{#4}$
}
\cs_new:Nn \sandu_solve:nnnn
{
\fp_eval:n { round( ( -(#3) #1 sqrt((#3)^2-4*(#2)*(#4)) )/(2*(#2)), 4) }
}
\ExplSyntaxOff

\begin{document}

\bigskip

\bigskip

\end{frame}
\end{document} Will also work with \addterm -5x in addition to the intended \addterm\cb x.

The \addterm macro takes a single argument, expands it once, and passes it to \addtermaux. The \addtermaux definition will grab the first token of the argument and examine to see if it is a minus - character. If so, it typesets a - and the rest of the argument. If not, it sees whether the first token was a + character. If so, it typesets a + and the rest of the argument. If neither of the above cases apply, it typesets a + and the complete argument.

In this way, the right output is provided whether \cc is set to 6 or set to +6.

\documentclass{beamer}
\usepackage{fp}
\begin{document}
\FPset\ca{1}
\FPset\cb{-5}
\FPset\cc{6}
\FPqsolve\xone\xtwo\ca\cb\cc
\FPeval\xone{clip(round(xone:4))}
\FPeval\xtwo{clip(round(xtwo:4))}
Quadratic equation : $\ca x^2 \addterm\cb x \addterm\cc=0$\\[1cm]
Result: $x = \xone \quad \text{and} \quad x = \xtwo$
\end{frame}
\end{document} • could you explain \newcommand and \def... – sandu Mar 20 at 11:27
• @sandu I have edited the answer to provide context. – Steven B. Segletes Mar 20 at 11:32

Edit: See below an improved version.

Note the [fragile] in \begin{frame}. Necessary with \FPifpos.

\documentclass{beamer}
\usepackage{fp}
\begin{document}
\FPset\ca{1}
\FPset\cb{-5}
\FPset\cc{6}
\FPqsolve\xone\xtwo\ca\cb\cc
\FPeval\xone{clip(round(xone:4))}
\FPeval\xtwo{clip(round(xtwo:4))}
\FPeval\babs{clip(round(abs(cb):4))}
\FPeval\cabs{clip(round(abs(cc):4))}

Quadratic equation :  $\ca x^2$ \FPifpos\cb $+$ \else $-$ \fi $\babs x$ \FPifpos\cc $+$ \else $-$ \fi $\cabs=0$ %\\[1cm]

Result: $x = \xone \quad \text{and} \quad x = \xtwo$

\end{frame}
\end{document} Improved version

This version handle better special situations (when some coefficients of the equation became -1, 1 or 0).

As fp's \FPqsolve doesn't handle equations without solutions (it emit an error), my code don't display correctly equations where the "x^2" AND the "x" term are null (it display, when the compiler don't stop at errors, something like: Quadratic equation: +6 = 0). This code is intended to be used only when the equation has real(s) solution(s).

\documentclass{beamer}
\usepackage{fp}
\begin{document}
\FPset\ca{1}
\FPset\cb{-5}
\FPset\cc{6}
\FPqsolve\xone\xtwo\ca\cb\cc
\FPeval\xone{clip(round(xone:4))}
\FPeval\xtwo{clip(round(xtwo:4))}
\FPeval\aabs{clip(round(abs(ca):4))}
\FPeval\babs{clip(round(abs(cb):4))}
\FPeval\cabs{clip(round(abs(cc):4))}
\newcommand{\signa}{\FPifneg\ca -\else\fi}
\newcommand{\positiveSignBWithA}{\FPifzero\ca \else +\fi} % if \ca is 0, no positive sign before the "x" term if cb is positive
\newcommand{\signb}{\FPifneg\cb -\else \positiveSignBWithA\fi}
\newcommand{\signc}{\FPifneg\cc -\else +\fi}
\newcommand{\coeffa}{\FPifeq\aabs1 \else\aabs\fi}
\newcommand{\coeffb}{\FPifeq\babs1 \else\babs\fi}
\newcommand{\polya}{\FPifzero\ca \else\signa\coeffa x^2\fi}
\newcommand{\polyb}{\FPifzero\cb \else\signb\coeffb x\fi}
\newcommand{\polyc}{\FPifzero\cc \else\signc\cabs\fi}

Quadratic equation : $\polya \polyb \polyc =0$ \\[1cm]

Result: $x = \xone \quad \text{and} \quad x = \xtwo$

\end{frame}
\end{document} 