Quadratic roots, narrow range

Percentage Accurate: 55.8% → 99.3%
Time: 9.5s
Alternatives: 6
Speedup: 3.6×

Specification

?
\[\left(\left(1.0536712127723509 \cdot 10^{-8} < a \land a < 94906265.62425156\right) \land \left(1.0536712127723509 \cdot 10^{-8} < b \land b < 94906265.62425156\right)\right) \land \left(1.0536712127723509 \cdot 10^{-8} < c \land c < 94906265.62425156\right)\]
\[\begin{array}{l} \\ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
double code(double a, double b, double c) {
	return (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
real(8) function code(a, b, c)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    code = (-b + sqrt(((b * b) - ((4.0d0 * a) * c)))) / (2.0d0 * a)
end function
public static double code(double a, double b, double c) {
	return (-b + Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
def code(a, b, c):
	return (-b + math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
function code(a, b, c)
	return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a))
end
function tmp = code(a, b, c)
	tmp = (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 6 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 55.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
double code(double a, double b, double c) {
	return (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
real(8) function code(a, b, c)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    code = (-b + sqrt(((b * b) - ((4.0d0 * a) * c)))) / (2.0d0 * a)
end function
public static double code(double a, double b, double c) {
	return (-b + Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
def code(a, b, c):
	return (-b + math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
function code(a, b, c)
	return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a))
end
function tmp = code(a, b, c)
	tmp = (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\end{array}

Alternative 1: 99.3% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \frac{\frac{-c}{a}}{\frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} - \frac{\frac{b}{a}}{-2}} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (/
  (/ (- c) a)
  (- (/ (sqrt (* (fma -4.0 a (/ (* b b) c)) c)) (* 2.0 a)) (/ (/ b a) -2.0))))
double code(double a, double b, double c) {
	return (-c / a) / ((sqrt((fma(-4.0, a, ((b * b) / c)) * c)) / (2.0 * a)) - ((b / a) / -2.0));
}
function code(a, b, c)
	return Float64(Float64(Float64(-c) / a) / Float64(Float64(sqrt(Float64(fma(-4.0, a, Float64(Float64(b * b) / c)) * c)) / Float64(2.0 * a)) - Float64(Float64(b / a) / -2.0)))
end
code[a_, b_, c_] := N[(N[((-c) / a), $MachinePrecision] / N[(N[(N[Sqrt[N[(N[(-4.0 * a + N[(N[(b * b), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision] * c), $MachinePrecision]], $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision] - N[(N[(b / a), $MachinePrecision] / -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\frac{-c}{a}}{\frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} - \frac{\frac{b}{a}}{-2}}
\end{array}
Derivation
  1. Initial program 57.3%

    \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
  2. Add Preprocessing
  3. Taylor expanded in c around inf

    \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{c \cdot \left(\frac{{b}^{2}}{c} - 4 \cdot a\right)}}}{2 \cdot a} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\left(\frac{{b}^{2}}{c} - 4 \cdot a\right) \cdot c}}}{2 \cdot a} \]
    2. lower-*.f64N/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\left(\frac{{b}^{2}}{c} - 4 \cdot a\right) \cdot c}}}{2 \cdot a} \]
    3. fp-cancel-sub-sign-invN/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\left(\frac{{b}^{2}}{c} + \left(\mathsf{neg}\left(4\right)\right) \cdot a\right)} \cdot c}}{2 \cdot a} \]
    4. metadata-evalN/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\left(\frac{{b}^{2}}{c} + \color{blue}{-4} \cdot a\right) \cdot c}}{2 \cdot a} \]
    5. +-commutativeN/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\left(-4 \cdot a + \frac{{b}^{2}}{c}\right)} \cdot c}}{2 \cdot a} \]
    6. lower-fma.f64N/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\mathsf{fma}\left(-4, a, \frac{{b}^{2}}{c}\right)} \cdot c}}{2 \cdot a} \]
    7. lower-/.f64N/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \color{blue}{\frac{{b}^{2}}{c}}\right) \cdot c}}{2 \cdot a} \]
    8. unpow2N/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \frac{\color{blue}{b \cdot b}}{c}\right) \cdot c}}{2 \cdot a} \]
    9. lower-*.f6457.3

      \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \frac{\color{blue}{b \cdot b}}{c}\right) \cdot c}}{2 \cdot a} \]
  5. Applied rewrites57.3%

    \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}}{2 \cdot a} \]
  6. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a}} \]
    2. lift-+.f64N/A

      \[\leadsto \frac{\color{blue}{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}}{2 \cdot a} \]
    3. div-addN/A

      \[\leadsto \color{blue}{\frac{-b}{2 \cdot a} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a}} \]
    4. lower-+.f64N/A

      \[\leadsto \color{blue}{\frac{-b}{2 \cdot a} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a}} \]
    5. lift-*.f64N/A

      \[\leadsto \frac{-b}{\color{blue}{2 \cdot a}} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} \]
    6. *-commutativeN/A

      \[\leadsto \frac{-b}{\color{blue}{a \cdot 2}} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} \]
    7. associate-/r*N/A

      \[\leadsto \color{blue}{\frac{\frac{-b}{a}}{2}} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} \]
    8. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{\frac{-b}{a}}{2}} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} \]
    9. lower-/.f64N/A

      \[\leadsto \frac{\color{blue}{\frac{-b}{a}}}{2} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} \]
    10. lower-/.f6456.3

      \[\leadsto \frac{\frac{-b}{a}}{2} + \color{blue}{\frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a}} \]
  7. Applied rewrites56.3%

    \[\leadsto \color{blue}{\frac{\frac{-b}{a}}{2} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a}} \]
  8. Applied rewrites57.3%

    \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}{{\left(-2 \cdot a\right)}^{2}} - {\left(\frac{\frac{b}{a}}{-2}\right)}^{2}}{\frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} - \frac{\frac{b}{a}}{-2}}} \]
  9. Taylor expanded in a around 0

    \[\leadsto \frac{\color{blue}{-1 \cdot \frac{c}{a}}}{\frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} - \frac{\frac{b}{a}}{-2}} \]
  10. Step-by-step derivation
    1. associate-*r/N/A

      \[\leadsto \frac{\color{blue}{\frac{-1 \cdot c}{a}}}{\frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} - \frac{\frac{b}{a}}{-2}} \]
    2. mul-1-negN/A

      \[\leadsto \frac{\frac{\color{blue}{\mathsf{neg}\left(c\right)}}{a}}{\frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} - \frac{\frac{b}{a}}{-2}} \]
    3. lower-/.f64N/A

      \[\leadsto \frac{\color{blue}{\frac{\mathsf{neg}\left(c\right)}{a}}}{\frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} - \frac{\frac{b}{a}}{-2}} \]
    4. lower-neg.f6499.3

      \[\leadsto \frac{\frac{\color{blue}{-c}}{a}}{\frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} - \frac{\frac{b}{a}}{-2}} \]
  11. Applied rewrites99.3%

    \[\leadsto \frac{\color{blue}{\frac{-c}{a}}}{\frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} - \frac{\frac{b}{a}}{-2}} \]
  12. Add Preprocessing

Alternative 2: 84.9% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c\\ \mathbf{if}\;b \leq 75:\\ \;\;\;\;\frac{-\left(b \cdot b - t\_0\right)}{\left(b + \sqrt{t\_0}\right) \cdot \left(2 \cdot a\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{-\mathsf{fma}\left(\frac{c \cdot c}{b}, \frac{a}{b}, c\right)}{b}\\ \end{array} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (let* ((t_0 (* (fma -4.0 a (/ (* b b) c)) c)))
   (if (<= b 75.0)
     (/ (- (- (* b b) t_0)) (* (+ b (sqrt t_0)) (* 2.0 a)))
     (/ (- (fma (/ (* c c) b) (/ a b) c)) b))))
double code(double a, double b, double c) {
	double t_0 = fma(-4.0, a, ((b * b) / c)) * c;
	double tmp;
	if (b <= 75.0) {
		tmp = -((b * b) - t_0) / ((b + sqrt(t_0)) * (2.0 * a));
	} else {
		tmp = -fma(((c * c) / b), (a / b), c) / b;
	}
	return tmp;
}
function code(a, b, c)
	t_0 = Float64(fma(-4.0, a, Float64(Float64(b * b) / c)) * c)
	tmp = 0.0
	if (b <= 75.0)
		tmp = Float64(Float64(-Float64(Float64(b * b) - t_0)) / Float64(Float64(b + sqrt(t_0)) * Float64(2.0 * a)));
	else
		tmp = Float64(Float64(-fma(Float64(Float64(c * c) / b), Float64(a / b), c)) / b);
	end
	return tmp
end
code[a_, b_, c_] := Block[{t$95$0 = N[(N[(-4.0 * a + N[(N[(b * b), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision] * c), $MachinePrecision]}, If[LessEqual[b, 75.0], N[((-N[(N[(b * b), $MachinePrecision] - t$95$0), $MachinePrecision]) / N[(N[(b + N[Sqrt[t$95$0], $MachinePrecision]), $MachinePrecision] * N[(2.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-N[(N[(N[(c * c), $MachinePrecision] / b), $MachinePrecision] * N[(a / b), $MachinePrecision] + c), $MachinePrecision]) / b), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c\\
\mathbf{if}\;b \leq 75:\\
\;\;\;\;\frac{-\left(b \cdot b - t\_0\right)}{\left(b + \sqrt{t\_0}\right) \cdot \left(2 \cdot a\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{-\mathsf{fma}\left(\frac{c \cdot c}{b}, \frac{a}{b}, c\right)}{b}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if b < 75

    1. Initial program 82.1%

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    2. Add Preprocessing
    3. Taylor expanded in c around inf

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{c \cdot \left(\frac{{b}^{2}}{c} - 4 \cdot a\right)}}}{2 \cdot a} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\left(\frac{{b}^{2}}{c} - 4 \cdot a\right) \cdot c}}}{2 \cdot a} \]
      2. lower-*.f64N/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\left(\frac{{b}^{2}}{c} - 4 \cdot a\right) \cdot c}}}{2 \cdot a} \]
      3. fp-cancel-sub-sign-invN/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\left(\frac{{b}^{2}}{c} + \left(\mathsf{neg}\left(4\right)\right) \cdot a\right)} \cdot c}}{2 \cdot a} \]
      4. metadata-evalN/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\left(\frac{{b}^{2}}{c} + \color{blue}{-4} \cdot a\right) \cdot c}}{2 \cdot a} \]
      5. +-commutativeN/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\left(-4 \cdot a + \frac{{b}^{2}}{c}\right)} \cdot c}}{2 \cdot a} \]
      6. lower-fma.f64N/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\mathsf{fma}\left(-4, a, \frac{{b}^{2}}{c}\right)} \cdot c}}{2 \cdot a} \]
      7. lower-/.f64N/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \color{blue}{\frac{{b}^{2}}{c}}\right) \cdot c}}{2 \cdot a} \]
      8. unpow2N/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \frac{\color{blue}{b \cdot b}}{c}\right) \cdot c}}{2 \cdot a} \]
      9. lower-*.f6481.9

        \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \frac{\color{blue}{b \cdot b}}{c}\right) \cdot c}}{2 \cdot a} \]
    5. Applied rewrites81.9%

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}}{2 \cdot a} \]
    6. Step-by-step derivation
      1. lift-/.f64N/A

        \[\leadsto \color{blue}{\frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a}} \]
      2. lift-+.f64N/A

        \[\leadsto \frac{\color{blue}{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}}{2 \cdot a} \]
      3. flip-+N/A

        \[\leadsto \frac{\color{blue}{\frac{\left(-b\right) \cdot \left(-b\right) - \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c} \cdot \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{\left(-b\right) - \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}}}{2 \cdot a} \]
      4. associate-/l/N/A

        \[\leadsto \color{blue}{\frac{\left(-b\right) \cdot \left(-b\right) - \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c} \cdot \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{\left(\left(-b\right) - \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}\right) \cdot \left(2 \cdot a\right)}} \]
      5. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{\left(-b\right) \cdot \left(-b\right) - \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c} \cdot \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{\left(\left(-b\right) - \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}\right) \cdot \left(2 \cdot a\right)}} \]
    7. Applied rewrites83.0%

      \[\leadsto \color{blue}{\frac{b \cdot b - \mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}{\left(\left(-b\right) - \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}\right) \cdot \left(2 \cdot a\right)}} \]

    if 75 < b

    1. Initial program 48.3%

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    2. Add Preprocessing
    3. Taylor expanded in a around 0

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b} + -1 \cdot \frac{a \cdot {c}^{2}}{{b}^{3}}} \]
    4. Step-by-step derivation
      1. associate-*r/N/A

        \[\leadsto \color{blue}{\frac{-1 \cdot c}{b}} + -1 \cdot \frac{a \cdot {c}^{2}}{{b}^{3}} \]
      2. unpow3N/A

        \[\leadsto \frac{-1 \cdot c}{b} + -1 \cdot \frac{a \cdot {c}^{2}}{\color{blue}{\left(b \cdot b\right) \cdot b}} \]
      3. unpow2N/A

        \[\leadsto \frac{-1 \cdot c}{b} + -1 \cdot \frac{a \cdot {c}^{2}}{\color{blue}{{b}^{2}} \cdot b} \]
      4. associate-/r*N/A

        \[\leadsto \frac{-1 \cdot c}{b} + -1 \cdot \color{blue}{\frac{\frac{a \cdot {c}^{2}}{{b}^{2}}}{b}} \]
      5. associate-/l*N/A

        \[\leadsto \frac{-1 \cdot c}{b} + \color{blue}{\frac{-1 \cdot \frac{a \cdot {c}^{2}}{{b}^{2}}}{b}} \]
      6. div-addN/A

        \[\leadsto \color{blue}{\frac{-1 \cdot c + -1 \cdot \frac{a \cdot {c}^{2}}{{b}^{2}}}{b}} \]
      7. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{-1 \cdot c + -1 \cdot \frac{a \cdot {c}^{2}}{{b}^{2}}}{b}} \]
    5. Applied rewrites88.5%

      \[\leadsto \color{blue}{\frac{-\mathsf{fma}\left(\frac{c \cdot c}{b}, \frac{a}{b}, c\right)}{b}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification87.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq 75:\\ \;\;\;\;\frac{-\left(b \cdot b - \mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c\right)}{\left(b + \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}\right) \cdot \left(2 \cdot a\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{-\mathsf{fma}\left(\frac{c \cdot c}{b}, \frac{a}{b}, c\right)}{b}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 84.7% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;b \leq 75:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(b, b, \left(-4 \cdot a\right) \cdot c\right)}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{-\mathsf{fma}\left(\frac{c \cdot c}{b}, \frac{a}{b}, c\right)}{b}\\ \end{array} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (if (<= b 75.0)
   (/ (+ (- b) (sqrt (fma b b (* (* -4.0 a) c)))) (* 2.0 a))
   (/ (- (fma (/ (* c c) b) (/ a b) c)) b)))
double code(double a, double b, double c) {
	double tmp;
	if (b <= 75.0) {
		tmp = (-b + sqrt(fma(b, b, ((-4.0 * a) * c)))) / (2.0 * a);
	} else {
		tmp = -fma(((c * c) / b), (a / b), c) / b;
	}
	return tmp;
}
function code(a, b, c)
	tmp = 0.0
	if (b <= 75.0)
		tmp = Float64(Float64(Float64(-b) + sqrt(fma(b, b, Float64(Float64(-4.0 * a) * c)))) / Float64(2.0 * a));
	else
		tmp = Float64(Float64(-fma(Float64(Float64(c * c) / b), Float64(a / b), c)) / b);
	end
	return tmp
end
code[a_, b_, c_] := If[LessEqual[b, 75.0], N[(N[((-b) + N[Sqrt[N[(b * b + N[(N[(-4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], N[((-N[(N[(N[(c * c), $MachinePrecision] / b), $MachinePrecision] * N[(a / b), $MachinePrecision] + c), $MachinePrecision]) / b), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;b \leq 75:\\
\;\;\;\;\frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(b, b, \left(-4 \cdot a\right) \cdot c\right)}}{2 \cdot a}\\

\mathbf{else}:\\
\;\;\;\;\frac{-\mathsf{fma}\left(\frac{c \cdot c}{b}, \frac{a}{b}, c\right)}{b}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if b < 75

    1. Initial program 82.1%

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift--.f64N/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{b \cdot b - \left(4 \cdot a\right) \cdot c}}}{2 \cdot a} \]
      2. lift-*.f64N/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{b \cdot b - \color{blue}{\left(4 \cdot a\right) \cdot c}}}{2 \cdot a} \]
      3. fp-cancel-sub-sign-invN/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{b \cdot b + \left(\mathsf{neg}\left(4 \cdot a\right)\right) \cdot c}}}{2 \cdot a} \]
      4. lift-*.f64N/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{b \cdot b} + \left(\mathsf{neg}\left(4 \cdot a\right)\right) \cdot c}}{2 \cdot a} \]
      5. lower-fma.f64N/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\mathsf{fma}\left(b, b, \left(\mathsf{neg}\left(4 \cdot a\right)\right) \cdot c\right)}}}{2 \cdot a} \]
      6. lower-*.f64N/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(b, b, \color{blue}{\left(\mathsf{neg}\left(4 \cdot a\right)\right) \cdot c}\right)}}{2 \cdot a} \]
      7. lift-*.f64N/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(b, b, \left(\mathsf{neg}\left(\color{blue}{4 \cdot a}\right)\right) \cdot c\right)}}{2 \cdot a} \]
      8. distribute-lft-neg-inN/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(b, b, \color{blue}{\left(\left(\mathsf{neg}\left(4\right)\right) \cdot a\right)} \cdot c\right)}}{2 \cdot a} \]
      9. lower-*.f64N/A

        \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(b, b, \color{blue}{\left(\left(\mathsf{neg}\left(4\right)\right) \cdot a\right)} \cdot c\right)}}{2 \cdot a} \]
      10. metadata-eval82.4

        \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(b, b, \left(\color{blue}{-4} \cdot a\right) \cdot c\right)}}{2 \cdot a} \]
    4. Applied rewrites82.4%

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\mathsf{fma}\left(b, b, \left(-4 \cdot a\right) \cdot c\right)}}}{2 \cdot a} \]

    if 75 < b

    1. Initial program 48.3%

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    2. Add Preprocessing
    3. Taylor expanded in a around 0

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b} + -1 \cdot \frac{a \cdot {c}^{2}}{{b}^{3}}} \]
    4. Step-by-step derivation
      1. associate-*r/N/A

        \[\leadsto \color{blue}{\frac{-1 \cdot c}{b}} + -1 \cdot \frac{a \cdot {c}^{2}}{{b}^{3}} \]
      2. unpow3N/A

        \[\leadsto \frac{-1 \cdot c}{b} + -1 \cdot \frac{a \cdot {c}^{2}}{\color{blue}{\left(b \cdot b\right) \cdot b}} \]
      3. unpow2N/A

        \[\leadsto \frac{-1 \cdot c}{b} + -1 \cdot \frac{a \cdot {c}^{2}}{\color{blue}{{b}^{2}} \cdot b} \]
      4. associate-/r*N/A

        \[\leadsto \frac{-1 \cdot c}{b} + -1 \cdot \color{blue}{\frac{\frac{a \cdot {c}^{2}}{{b}^{2}}}{b}} \]
      5. associate-/l*N/A

        \[\leadsto \frac{-1 \cdot c}{b} + \color{blue}{\frac{-1 \cdot \frac{a \cdot {c}^{2}}{{b}^{2}}}{b}} \]
      6. div-addN/A

        \[\leadsto \color{blue}{\frac{-1 \cdot c + -1 \cdot \frac{a \cdot {c}^{2}}{{b}^{2}}}{b}} \]
      7. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{-1 \cdot c + -1 \cdot \frac{a \cdot {c}^{2}}{{b}^{2}}}{b}} \]
    5. Applied rewrites88.5%

      \[\leadsto \color{blue}{\frac{-\mathsf{fma}\left(\frac{c \cdot c}{b}, \frac{a}{b}, c\right)}{b}} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 81.0% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \frac{-\mathsf{fma}\left(\frac{c \cdot c}{b}, \frac{a}{b}, c\right)}{b} \end{array} \]
(FPCore (a b c) :precision binary64 (/ (- (fma (/ (* c c) b) (/ a b) c)) b))
double code(double a, double b, double c) {
	return -fma(((c * c) / b), (a / b), c) / b;
}
function code(a, b, c)
	return Float64(Float64(-fma(Float64(Float64(c * c) / b), Float64(a / b), c)) / b)
end
code[a_, b_, c_] := N[((-N[(N[(N[(c * c), $MachinePrecision] / b), $MachinePrecision] * N[(a / b), $MachinePrecision] + c), $MachinePrecision]) / b), $MachinePrecision]
\begin{array}{l}

\\
\frac{-\mathsf{fma}\left(\frac{c \cdot c}{b}, \frac{a}{b}, c\right)}{b}
\end{array}
Derivation
  1. Initial program 57.3%

    \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
  2. Add Preprocessing
  3. Taylor expanded in a around 0

    \[\leadsto \color{blue}{-1 \cdot \frac{c}{b} + -1 \cdot \frac{a \cdot {c}^{2}}{{b}^{3}}} \]
  4. Step-by-step derivation
    1. associate-*r/N/A

      \[\leadsto \color{blue}{\frac{-1 \cdot c}{b}} + -1 \cdot \frac{a \cdot {c}^{2}}{{b}^{3}} \]
    2. unpow3N/A

      \[\leadsto \frac{-1 \cdot c}{b} + -1 \cdot \frac{a \cdot {c}^{2}}{\color{blue}{\left(b \cdot b\right) \cdot b}} \]
    3. unpow2N/A

      \[\leadsto \frac{-1 \cdot c}{b} + -1 \cdot \frac{a \cdot {c}^{2}}{\color{blue}{{b}^{2}} \cdot b} \]
    4. associate-/r*N/A

      \[\leadsto \frac{-1 \cdot c}{b} + -1 \cdot \color{blue}{\frac{\frac{a \cdot {c}^{2}}{{b}^{2}}}{b}} \]
    5. associate-/l*N/A

      \[\leadsto \frac{-1 \cdot c}{b} + \color{blue}{\frac{-1 \cdot \frac{a \cdot {c}^{2}}{{b}^{2}}}{b}} \]
    6. div-addN/A

      \[\leadsto \color{blue}{\frac{-1 \cdot c + -1 \cdot \frac{a \cdot {c}^{2}}{{b}^{2}}}{b}} \]
    7. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{-1 \cdot c + -1 \cdot \frac{a \cdot {c}^{2}}{{b}^{2}}}{b}} \]
  5. Applied rewrites80.4%

    \[\leadsto \color{blue}{\frac{-\mathsf{fma}\left(\frac{c \cdot c}{b}, \frac{a}{b}, c\right)}{b}} \]
  6. Add Preprocessing

Alternative 5: 63.9% accurate, 3.6× speedup?

\[\begin{array}{l} \\ \frac{-c}{b} \end{array} \]
(FPCore (a b c) :precision binary64 (/ (- c) b))
double code(double a, double b, double c) {
	return -c / b;
}
real(8) function code(a, b, c)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    code = -c / b
end function
public static double code(double a, double b, double c) {
	return -c / b;
}
def code(a, b, c):
	return -c / b
function code(a, b, c)
	return Float64(Float64(-c) / b)
end
function tmp = code(a, b, c)
	tmp = -c / b;
end
code[a_, b_, c_] := N[((-c) / b), $MachinePrecision]
\begin{array}{l}

\\
\frac{-c}{b}
\end{array}
Derivation
  1. Initial program 57.3%

    \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
  2. Add Preprocessing
  3. Taylor expanded in a around 0

    \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}} \]
  4. Step-by-step derivation
    1. associate-*r/N/A

      \[\leadsto \color{blue}{\frac{-1 \cdot c}{b}} \]
    2. mul-1-negN/A

      \[\leadsto \frac{\color{blue}{\mathsf{neg}\left(c\right)}}{b} \]
    3. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{\mathsf{neg}\left(c\right)}{b}} \]
    4. lower-neg.f6462.9

      \[\leadsto \frac{\color{blue}{-c}}{b} \]
  5. Applied rewrites62.9%

    \[\leadsto \color{blue}{\frac{-c}{b}} \]
  6. Add Preprocessing

Alternative 6: 0.0% accurate, 6.3× speedup?

\[\begin{array}{l} \\ \left(-b\right) \cdot \mathsf{NAN}\left(\right) \end{array} \]
(FPCore (a b c) :precision binary64 (* (- b) (NAN)))
\begin{array}{l}

\\
\left(-b\right) \cdot \mathsf{NAN}\left(\right)
\end{array}
Derivation
  1. Initial program 57.3%

    \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
  2. Add Preprocessing
  3. Taylor expanded in c around inf

    \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{c \cdot \left(\frac{{b}^{2}}{c} - 4 \cdot a\right)}}}{2 \cdot a} \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\left(\frac{{b}^{2}}{c} - 4 \cdot a\right) \cdot c}}}{2 \cdot a} \]
    2. lower-*.f64N/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\left(\frac{{b}^{2}}{c} - 4 \cdot a\right) \cdot c}}}{2 \cdot a} \]
    3. fp-cancel-sub-sign-invN/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\left(\frac{{b}^{2}}{c} + \left(\mathsf{neg}\left(4\right)\right) \cdot a\right)} \cdot c}}{2 \cdot a} \]
    4. metadata-evalN/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\left(\frac{{b}^{2}}{c} + \color{blue}{-4} \cdot a\right) \cdot c}}{2 \cdot a} \]
    5. +-commutativeN/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\left(-4 \cdot a + \frac{{b}^{2}}{c}\right)} \cdot c}}{2 \cdot a} \]
    6. lower-fma.f64N/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\mathsf{fma}\left(-4, a, \frac{{b}^{2}}{c}\right)} \cdot c}}{2 \cdot a} \]
    7. lower-/.f64N/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \color{blue}{\frac{{b}^{2}}{c}}\right) \cdot c}}{2 \cdot a} \]
    8. unpow2N/A

      \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \frac{\color{blue}{b \cdot b}}{c}\right) \cdot c}}{2 \cdot a} \]
    9. lower-*.f6457.3

      \[\leadsto \frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \frac{\color{blue}{b \cdot b}}{c}\right) \cdot c}}{2 \cdot a} \]
  5. Applied rewrites57.3%

    \[\leadsto \frac{\left(-b\right) + \sqrt{\color{blue}{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}}{2 \cdot a} \]
  6. Step-by-step derivation
    1. lift-/.f64N/A

      \[\leadsto \color{blue}{\frac{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a}} \]
    2. lift-+.f64N/A

      \[\leadsto \frac{\color{blue}{\left(-b\right) + \sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}}{2 \cdot a} \]
    3. div-addN/A

      \[\leadsto \color{blue}{\frac{-b}{2 \cdot a} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a}} \]
    4. lower-+.f64N/A

      \[\leadsto \color{blue}{\frac{-b}{2 \cdot a} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a}} \]
    5. lift-*.f64N/A

      \[\leadsto \frac{-b}{\color{blue}{2 \cdot a}} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} \]
    6. *-commutativeN/A

      \[\leadsto \frac{-b}{\color{blue}{a \cdot 2}} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} \]
    7. associate-/r*N/A

      \[\leadsto \color{blue}{\frac{\frac{-b}{a}}{2}} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} \]
    8. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{\frac{-b}{a}}{2}} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} \]
    9. lower-/.f64N/A

      \[\leadsto \frac{\color{blue}{\frac{-b}{a}}}{2} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} \]
    10. lower-/.f6456.3

      \[\leadsto \frac{\frac{-b}{a}}{2} + \color{blue}{\frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a}} \]
  7. Applied rewrites56.3%

    \[\leadsto \color{blue}{\frac{\frac{-b}{a}}{2} + \frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a}} \]
  8. Applied rewrites57.3%

    \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}{{\left(-2 \cdot a\right)}^{2}} - {\left(\frac{\frac{b}{a}}{-2}\right)}^{2}}{\frac{\sqrt{\mathsf{fma}\left(-4, a, \frac{b \cdot b}{c}\right) \cdot c}}{2 \cdot a} - \frac{\frac{b}{a}}{-2}}} \]
  9. Taylor expanded in b around -inf

    \[\leadsto \color{blue}{-1 \cdot \left(b \cdot \mathsf{NAN}\left(\right)\right)} \]
  10. Step-by-step derivation
    1. associate-*r*N/A

      \[\leadsto \color{blue}{\left(-1 \cdot b\right) \cdot \mathsf{NAN}\left(\right)} \]
    2. mul-1-negN/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(b\right)\right)} \cdot \mathsf{NAN}\left(\right) \]
    3. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(b\right)\right) \cdot \mathsf{NAN}\left(\right)} \]
    4. lower-neg.f64N/A

      \[\leadsto \color{blue}{\left(-b\right)} \cdot \mathsf{NAN}\left(\right) \]
    5. lower-NAN.f640.0

      \[\leadsto \left(-b\right) \cdot \color{blue}{\mathsf{NAN}\left(\right)} \]
  11. Applied rewrites0.0%

    \[\leadsto \color{blue}{\left(-b\right) \cdot \mathsf{NAN}\left(\right)} \]
  12. Add Preprocessing

Reproduce

?
herbie shell --seed 2024326 
(FPCore (a b c)
  :name "Quadratic roots, narrow range"
  :precision binary64
  :pre (and (and (and (< 1.0536712127723509e-8 a) (< a 94906265.62425156)) (and (< 1.0536712127723509e-8 b) (< b 94906265.62425156))) (and (< 1.0536712127723509e-8 c) (< c 94906265.62425156)))
  (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))