How to write Quadratic equation with negative coefficient

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

\documentclass{beamer}
\usepackage{fp}

\newcommand{\quadratic}[4][x]{%
  \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}
\begin{frame}{Quadratic equation}

\quadratic{1}{-5}{6}

\bigskip

\quadratic[t]{2}{3}{1}

\bigskip

\quadratic{2}{0}{-8}

\end{frame}
\end{document}

With expl3:

\documentclass{beamer}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\quadratic}{O{x}mmm}
 {
  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}
\begin{frame}{Quadratic equation}

\quadratic{1}{-5}{6}

\bigskip

\quadratic[t]{2}{3}{1}

\bigskip

\quadratic{2}{0}{-8}

\end{frame}
\end{document}

expl3


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}
\newcommand\addterm[1]{\expandafter\addtermaux#1\relax}
\def\addtermaux#1#2\relax{\ifx-#1-#2\else\ifx+#1+#2\else+#1#2\fi\fi}
\begin{document}
\begin{frame}{Quadratic equation}
\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}

enter image description here


Edit: See below an improved version.

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

\documentclass{beamer}
\usepackage{fp}
\begin{document}
\begin{frame}[fragile]{Quadratic equation}
\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}

enter image description here

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}
\begin{frame}[fragile]{Quadratic equation}
\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}

enter image description here

Tags:

Fp