Quadratic roots, full range

Percentage Accurate: 52.6% → 85.0%
Time: 13.9s
Alternatives: 8
Speedup: 19.1×

Specification

?
\[\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 8 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: 52.6% 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: 85.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;b \leq -2 \cdot 10^{+158}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq 6.8 \cdot 10^{-124}:\\ \;\;\;\;\frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)} - b}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (if (<= b -2e+158)
   (- (/ c b) (/ b a))
   (if (<= b 6.8e-124)
     (/ (- (sqrt (- (* b b) (* c (* a 4.0)))) b) (* a 2.0))
     (/ (- c) b))))
double code(double a, double b, double c) {
	double tmp;
	if (b <= -2e+158) {
		tmp = (c / b) - (b / a);
	} else if (b <= 6.8e-124) {
		tmp = (sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0);
	} else {
		tmp = -c / b;
	}
	return tmp;
}
real(8) function code(a, b, c)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8) :: tmp
    if (b <= (-2d+158)) then
        tmp = (c / b) - (b / a)
    else if (b <= 6.8d-124) then
        tmp = (sqrt(((b * b) - (c * (a * 4.0d0)))) - b) / (a * 2.0d0)
    else
        tmp = -c / b
    end if
    code = tmp
end function
public static double code(double a, double b, double c) {
	double tmp;
	if (b <= -2e+158) {
		tmp = (c / b) - (b / a);
	} else if (b <= 6.8e-124) {
		tmp = (Math.sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0);
	} else {
		tmp = -c / b;
	}
	return tmp;
}
def code(a, b, c):
	tmp = 0
	if b <= -2e+158:
		tmp = (c / b) - (b / a)
	elif b <= 6.8e-124:
		tmp = (math.sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0)
	else:
		tmp = -c / b
	return tmp
function code(a, b, c)
	tmp = 0.0
	if (b <= -2e+158)
		tmp = Float64(Float64(c / b) - Float64(b / a));
	elseif (b <= 6.8e-124)
		tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(c * Float64(a * 4.0)))) - b) / Float64(a * 2.0));
	else
		tmp = Float64(Float64(-c) / b);
	end
	return tmp
end
function tmp_2 = code(a, b, c)
	tmp = 0.0;
	if (b <= -2e+158)
		tmp = (c / b) - (b / a);
	elseif (b <= 6.8e-124)
		tmp = (sqrt(((b * b) - (c * (a * 4.0)))) - b) / (a * 2.0);
	else
		tmp = -c / b;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_] := If[LessEqual[b, -2e+158], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 6.8e-124], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(c * N[(a * 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;b \leq -2 \cdot 10^{+158}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\

\mathbf{elif}\;b \leq 6.8 \cdot 10^{-124}:\\
\;\;\;\;\frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)} - b}{a \cdot 2}\\

\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if b < -1.99999999999999991e158

    1. Initial program 52.3%

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

      \[\leadsto \color{blue}{-1 \cdot \frac{b}{a} + \frac{c}{b}} \]
    3. Step-by-step derivation
      1. +-commutative98.2%

        \[\leadsto \color{blue}{\frac{c}{b} + -1 \cdot \frac{b}{a}} \]
      2. mul-1-neg98.2%

        \[\leadsto \frac{c}{b} + \color{blue}{\left(-\frac{b}{a}\right)} \]
      3. unsub-neg98.2%

        \[\leadsto \color{blue}{\frac{c}{b} - \frac{b}{a}} \]
    4. Simplified98.2%

      \[\leadsto \color{blue}{\frac{c}{b} - \frac{b}{a}} \]

    if -1.99999999999999991e158 < b < 6.8000000000000001e-124

    1. Initial program 88.0%

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    2. Step-by-step derivation
      1. log1p-expm1-u_binary6444.0%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\right)\right)} \]
    3. Applied rewrite-once44.0%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\right)\right)} \]
    4. Step-by-step derivation
      1. log1p-expm188.0%

        \[\leadsto \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}} \]
      2. mul-1-neg88.0%

        \[\leadsto \frac{\color{blue}{-1 \cdot b} + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
      3. +-commutative88.0%

        \[\leadsto \frac{\color{blue}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} + -1 \cdot b}}{2 \cdot a} \]
      4. mul-1-neg88.0%

        \[\leadsto \frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} + \color{blue}{\left(-b\right)}}{2 \cdot a} \]
      5. unsub-neg88.0%

        \[\leadsto \frac{\color{blue}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}}{2 \cdot a} \]
      6. *-commutative88.0%

        \[\leadsto \frac{\sqrt{b \cdot b - \color{blue}{c \cdot \left(4 \cdot a\right)}} - b}{2 \cdot a} \]
      7. *-commutative88.0%

        \[\leadsto \frac{\sqrt{b \cdot b - c \cdot \color{blue}{\left(a \cdot 4\right)}} - b}{2 \cdot a} \]
      8. *-commutative88.0%

        \[\leadsto \frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)} - b}{\color{blue}{a \cdot 2}} \]
    5. Simplified88.0%

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

    if 6.8000000000000001e-124 < b

    1. Initial program 17.1%

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

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}} \]
    3. Step-by-step derivation
      1. associate-*r/87.5%

        \[\leadsto \color{blue}{\frac{-1 \cdot c}{b}} \]
      2. neg-mul-187.5%

        \[\leadsto \frac{\color{blue}{-c}}{b} \]
    4. Simplified87.5%

      \[\leadsto \color{blue}{\frac{-c}{b}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification89.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -2 \cdot 10^{+158}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq 6.8 \cdot 10^{-124}:\\ \;\;\;\;\frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)} - b}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \]

Alternative 2: 80.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;b \leq -2.9 \cdot 10^{-87}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq 1.82 \cdot 10^{-124}:\\ \;\;\;\;\frac{0.5}{a} \cdot \left(b + \sqrt{a \cdot \left(c \cdot -4\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (if (<= b -2.9e-87)
   (- (/ c b) (/ b a))
   (if (<= b 1.82e-124)
     (* (/ 0.5 a) (+ b (sqrt (* a (* c -4.0)))))
     (/ (- c) b))))
double code(double a, double b, double c) {
	double tmp;
	if (b <= -2.9e-87) {
		tmp = (c / b) - (b / a);
	} else if (b <= 1.82e-124) {
		tmp = (0.5 / a) * (b + sqrt((a * (c * -4.0))));
	} else {
		tmp = -c / b;
	}
	return tmp;
}
real(8) function code(a, b, c)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8) :: tmp
    if (b <= (-2.9d-87)) then
        tmp = (c / b) - (b / a)
    else if (b <= 1.82d-124) then
        tmp = (0.5d0 / a) * (b + sqrt((a * (c * (-4.0d0)))))
    else
        tmp = -c / b
    end if
    code = tmp
end function
public static double code(double a, double b, double c) {
	double tmp;
	if (b <= -2.9e-87) {
		tmp = (c / b) - (b / a);
	} else if (b <= 1.82e-124) {
		tmp = (0.5 / a) * (b + Math.sqrt((a * (c * -4.0))));
	} else {
		tmp = -c / b;
	}
	return tmp;
}
def code(a, b, c):
	tmp = 0
	if b <= -2.9e-87:
		tmp = (c / b) - (b / a)
	elif b <= 1.82e-124:
		tmp = (0.5 / a) * (b + math.sqrt((a * (c * -4.0))))
	else:
		tmp = -c / b
	return tmp
function code(a, b, c)
	tmp = 0.0
	if (b <= -2.9e-87)
		tmp = Float64(Float64(c / b) - Float64(b / a));
	elseif (b <= 1.82e-124)
		tmp = Float64(Float64(0.5 / a) * Float64(b + sqrt(Float64(a * Float64(c * -4.0)))));
	else
		tmp = Float64(Float64(-c) / b);
	end
	return tmp
end
function tmp_2 = code(a, b, c)
	tmp = 0.0;
	if (b <= -2.9e-87)
		tmp = (c / b) - (b / a);
	elseif (b <= 1.82e-124)
		tmp = (0.5 / a) * (b + sqrt((a * (c * -4.0))));
	else
		tmp = -c / b;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_] := If[LessEqual[b, -2.9e-87], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 1.82e-124], N[(N[(0.5 / a), $MachinePrecision] * N[(b + N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;b \leq -2.9 \cdot 10^{-87}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\

\mathbf{elif}\;b \leq 1.82 \cdot 10^{-124}:\\
\;\;\;\;\frac{0.5}{a} \cdot \left(b + \sqrt{a \cdot \left(c \cdot -4\right)}\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if b < -2.8999999999999999e-87

    1. Initial program 73.6%

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

      \[\leadsto \color{blue}{-1 \cdot \frac{b}{a} + \frac{c}{b}} \]
    3. Step-by-step derivation
      1. +-commutative87.5%

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

        \[\leadsto \frac{c}{b} + \color{blue}{\left(-\frac{b}{a}\right)} \]
      3. unsub-neg87.5%

        \[\leadsto \color{blue}{\frac{c}{b} - \frac{b}{a}} \]
    4. Simplified87.5%

      \[\leadsto \color{blue}{\frac{c}{b} - \frac{b}{a}} \]

    if -2.8999999999999999e-87 < b < 1.82000000000000009e-124

    1. Initial program 81.2%

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    2. Applied egg-rr74.1%

      \[\leadsto \color{blue}{\frac{0.5}{a} \cdot \left(b + \sqrt{\mathsf{fma}\left(b, b, a \cdot \left(-4 \cdot c\right)\right)}\right)} \]
    3. Step-by-step derivation
      1. *-commutative74.1%

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

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\mathsf{fma}\left(b, b, \color{blue}{\left(a \cdot c\right) \cdot -4}\right)}\right) \]
      3. metadata-eval74.1%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\mathsf{fma}\left(b, b, \left(a \cdot c\right) \cdot \color{blue}{\left(-4\right)}\right)}\right) \]
      4. distribute-rgt-neg-in74.1%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\mathsf{fma}\left(b, b, \color{blue}{-\left(a \cdot c\right) \cdot 4}\right)}\right) \]
      5. *-commutative74.1%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\mathsf{fma}\left(b, b, -\color{blue}{\left(c \cdot a\right)} \cdot 4\right)}\right) \]
      6. associate-*r*74.1%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\mathsf{fma}\left(b, b, -\color{blue}{c \cdot \left(a \cdot 4\right)}\right)}\right) \]
      7. fma-neg74.1%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\color{blue}{b \cdot b - c \cdot \left(a \cdot 4\right)}}\right) \]
    4. Applied egg-rr74.1%

      \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\color{blue}{b \cdot b - c \cdot \left(a \cdot 4\right)}}\right) \]
    5. Taylor expanded in b around 0 73.8%

      \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\color{blue}{-4 \cdot \left(a \cdot c\right)}}\right) \]
    6. Step-by-step derivation
      1. associate-*r*73.8%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\color{blue}{\left(-4 \cdot a\right) \cdot c}}\right) \]
      2. *-commutative73.8%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\color{blue}{\left(a \cdot -4\right)} \cdot c}\right) \]
      3. associate-*r*73.8%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\color{blue}{a \cdot \left(-4 \cdot c\right)}}\right) \]
      4. *-commutative73.8%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{a \cdot \color{blue}{\left(c \cdot -4\right)}}\right) \]
    7. Simplified73.8%

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

    if 1.82000000000000009e-124 < b

    1. Initial program 17.1%

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

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}} \]
    3. Step-by-step derivation
      1. associate-*r/87.5%

        \[\leadsto \color{blue}{\frac{-1 \cdot c}{b}} \]
      2. neg-mul-187.5%

        \[\leadsto \frac{\color{blue}{-c}}{b} \]
    4. Simplified87.5%

      \[\leadsto \color{blue}{\frac{-c}{b}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification84.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -2.9 \cdot 10^{-87}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq 1.82 \cdot 10^{-124}:\\ \;\;\;\;\frac{0.5}{a} \cdot \left(b + \sqrt{a \cdot \left(c \cdot -4\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \]

Alternative 3: 80.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;b \leq -6.5 \cdot 10^{-88}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq 1.32 \cdot 10^{-120}:\\ \;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (if (<= b -6.5e-88)
   (- (/ c b) (/ b a))
   (if (<= b 1.32e-120)
     (/ (- (sqrt (* a (* c -4.0))) b) (* a 2.0))
     (/ (- c) b))))
double code(double a, double b, double c) {
	double tmp;
	if (b <= -6.5e-88) {
		tmp = (c / b) - (b / a);
	} else if (b <= 1.32e-120) {
		tmp = (sqrt((a * (c * -4.0))) - b) / (a * 2.0);
	} else {
		tmp = -c / b;
	}
	return tmp;
}
real(8) function code(a, b, c)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8) :: tmp
    if (b <= (-6.5d-88)) then
        tmp = (c / b) - (b / a)
    else if (b <= 1.32d-120) then
        tmp = (sqrt((a * (c * (-4.0d0)))) - b) / (a * 2.0d0)
    else
        tmp = -c / b
    end if
    code = tmp
end function
public static double code(double a, double b, double c) {
	double tmp;
	if (b <= -6.5e-88) {
		tmp = (c / b) - (b / a);
	} else if (b <= 1.32e-120) {
		tmp = (Math.sqrt((a * (c * -4.0))) - b) / (a * 2.0);
	} else {
		tmp = -c / b;
	}
	return tmp;
}
def code(a, b, c):
	tmp = 0
	if b <= -6.5e-88:
		tmp = (c / b) - (b / a)
	elif b <= 1.32e-120:
		tmp = (math.sqrt((a * (c * -4.0))) - b) / (a * 2.0)
	else:
		tmp = -c / b
	return tmp
function code(a, b, c)
	tmp = 0.0
	if (b <= -6.5e-88)
		tmp = Float64(Float64(c / b) - Float64(b / a));
	elseif (b <= 1.32e-120)
		tmp = Float64(Float64(sqrt(Float64(a * Float64(c * -4.0))) - b) / Float64(a * 2.0));
	else
		tmp = Float64(Float64(-c) / b);
	end
	return tmp
end
function tmp_2 = code(a, b, c)
	tmp = 0.0;
	if (b <= -6.5e-88)
		tmp = (c / b) - (b / a);
	elseif (b <= 1.32e-120)
		tmp = (sqrt((a * (c * -4.0))) - b) / (a * 2.0);
	else
		tmp = -c / b;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_] := If[LessEqual[b, -6.5e-88], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], If[LessEqual[b, 1.32e-120], N[(N[(N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;b \leq -6.5 \cdot 10^{-88}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\

\mathbf{elif}\;b \leq 1.32 \cdot 10^{-120}:\\
\;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\

\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if b < -6.50000000000000006e-88

    1. Initial program 73.6%

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

      \[\leadsto \color{blue}{-1 \cdot \frac{b}{a} + \frac{c}{b}} \]
    3. Step-by-step derivation
      1. +-commutative87.5%

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

        \[\leadsto \frac{c}{b} + \color{blue}{\left(-\frac{b}{a}\right)} \]
      3. unsub-neg87.5%

        \[\leadsto \color{blue}{\frac{c}{b} - \frac{b}{a}} \]
    4. Simplified87.5%

      \[\leadsto \color{blue}{\frac{c}{b} - \frac{b}{a}} \]

    if -6.50000000000000006e-88 < b < 1.32000000000000004e-120

    1. Initial program 81.2%

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    2. Step-by-step derivation
      1. log1p-expm1-u_binary6449.3%

        \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\right)\right)} \]
    3. Applied rewrite-once49.3%

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\right)\right)} \]
    4. Step-by-step derivation
      1. log1p-expm181.2%

        \[\leadsto \color{blue}{\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}} \]
      2. mul-1-neg81.2%

        \[\leadsto \frac{\color{blue}{-1 \cdot b} + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
      3. +-commutative81.2%

        \[\leadsto \frac{\color{blue}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} + -1 \cdot b}}{2 \cdot a} \]
      4. mul-1-neg81.2%

        \[\leadsto \frac{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} + \color{blue}{\left(-b\right)}}{2 \cdot a} \]
      5. unsub-neg81.2%

        \[\leadsto \frac{\color{blue}{\sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c} - b}}{2 \cdot a} \]
      6. *-commutative81.2%

        \[\leadsto \frac{\sqrt{b \cdot b - \color{blue}{c \cdot \left(4 \cdot a\right)}} - b}{2 \cdot a} \]
      7. *-commutative81.2%

        \[\leadsto \frac{\sqrt{b \cdot b - c \cdot \color{blue}{\left(a \cdot 4\right)}} - b}{2 \cdot a} \]
      8. *-commutative81.2%

        \[\leadsto \frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)} - b}{\color{blue}{a \cdot 2}} \]
    5. Simplified81.2%

      \[\leadsto \color{blue}{\frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)} - b}{a \cdot 2}} \]
    6. Taylor expanded in b around 0 75.8%

      \[\leadsto \frac{\sqrt{\color{blue}{-4 \cdot \left(a \cdot c\right)}} - b}{a \cdot 2} \]
    7. Step-by-step derivation
      1. associate-*r*73.8%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\color{blue}{\left(-4 \cdot a\right) \cdot c}}\right) \]
      2. *-commutative73.8%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\color{blue}{\left(a \cdot -4\right)} \cdot c}\right) \]
      3. associate-*r*73.8%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{\color{blue}{a \cdot \left(-4 \cdot c\right)}}\right) \]
      4. *-commutative73.8%

        \[\leadsto \frac{0.5}{a} \cdot \left(b + \sqrt{a \cdot \color{blue}{\left(c \cdot -4\right)}}\right) \]
    8. Simplified75.8%

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

    if 1.32000000000000004e-120 < b

    1. Initial program 17.1%

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

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}} \]
    3. Step-by-step derivation
      1. associate-*r/87.5%

        \[\leadsto \color{blue}{\frac{-1 \cdot c}{b}} \]
      2. neg-mul-187.5%

        \[\leadsto \frac{\color{blue}{-c}}{b} \]
    4. Simplified87.5%

      \[\leadsto \color{blue}{\frac{-c}{b}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification85.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -6.5 \cdot 10^{-88}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq 1.32 \cdot 10^{-120}:\\ \;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \]

Alternative 4: 67.4% accurate, 12.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;b \leq -5 \cdot 10^{-310}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (if (<= b -5e-310) (- (/ c b) (/ b a)) (/ (- c) b)))
double code(double a, double b, double c) {
	double tmp;
	if (b <= -5e-310) {
		tmp = (c / b) - (b / a);
	} else {
		tmp = -c / b;
	}
	return tmp;
}
real(8) function code(a, b, c)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8) :: tmp
    if (b <= (-5d-310)) then
        tmp = (c / b) - (b / a)
    else
        tmp = -c / b
    end if
    code = tmp
end function
public static double code(double a, double b, double c) {
	double tmp;
	if (b <= -5e-310) {
		tmp = (c / b) - (b / a);
	} else {
		tmp = -c / b;
	}
	return tmp;
}
def code(a, b, c):
	tmp = 0
	if b <= -5e-310:
		tmp = (c / b) - (b / a)
	else:
		tmp = -c / b
	return tmp
function code(a, b, c)
	tmp = 0.0
	if (b <= -5e-310)
		tmp = Float64(Float64(c / b) - Float64(b / a));
	else
		tmp = Float64(Float64(-c) / b);
	end
	return tmp
end
function tmp_2 = code(a, b, c)
	tmp = 0.0;
	if (b <= -5e-310)
		tmp = (c / b) - (b / a);
	else
		tmp = -c / b;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_] := If[LessEqual[b, -5e-310], N[(N[(c / b), $MachinePrecision] - N[(b / a), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;b \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\frac{c}{b} - \frac{b}{a}\\

\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if b < -4.999999999999985e-310

    1. Initial program 75.6%

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

      \[\leadsto \color{blue}{-1 \cdot \frac{b}{a} + \frac{c}{b}} \]
    3. Step-by-step derivation
      1. +-commutative70.0%

        \[\leadsto \color{blue}{\frac{c}{b} + -1 \cdot \frac{b}{a}} \]
      2. mul-1-neg70.0%

        \[\leadsto \frac{c}{b} + \color{blue}{\left(-\frac{b}{a}\right)} \]
      3. unsub-neg70.0%

        \[\leadsto \color{blue}{\frac{c}{b} - \frac{b}{a}} \]
    4. Simplified70.0%

      \[\leadsto \color{blue}{\frac{c}{b} - \frac{b}{a}} \]

    if -4.999999999999985e-310 < b

    1. Initial program 25.6%

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

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}} \]
    3. Step-by-step derivation
      1. associate-*r/77.4%

        \[\leadsto \color{blue}{\frac{-1 \cdot c}{b}} \]
      2. neg-mul-177.4%

        \[\leadsto \frac{\color{blue}{-c}}{b} \]
    4. Simplified77.4%

      \[\leadsto \color{blue}{\frac{-c}{b}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification73.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq -5 \cdot 10^{-310}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \]

Alternative 5: 42.9% accurate, 19.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;b \leq 0.00042:\\ \;\;\;\;\frac{-b}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b}\\ \end{array} \end{array} \]
(FPCore (a b c) :precision binary64 (if (<= b 0.00042) (/ (- b) a) (/ c b)))
double code(double a, double b, double c) {
	double tmp;
	if (b <= 0.00042) {
		tmp = -b / a;
	} else {
		tmp = c / b;
	}
	return tmp;
}
real(8) function code(a, b, c)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8) :: tmp
    if (b <= 0.00042d0) then
        tmp = -b / a
    else
        tmp = c / b
    end if
    code = tmp
end function
public static double code(double a, double b, double c) {
	double tmp;
	if (b <= 0.00042) {
		tmp = -b / a;
	} else {
		tmp = c / b;
	}
	return tmp;
}
def code(a, b, c):
	tmp = 0
	if b <= 0.00042:
		tmp = -b / a
	else:
		tmp = c / b
	return tmp
function code(a, b, c)
	tmp = 0.0
	if (b <= 0.00042)
		tmp = Float64(Float64(-b) / a);
	else
		tmp = Float64(c / b);
	end
	return tmp
end
function tmp_2 = code(a, b, c)
	tmp = 0.0;
	if (b <= 0.00042)
		tmp = -b / a;
	else
		tmp = c / b;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_] := If[LessEqual[b, 0.00042], N[((-b) / a), $MachinePrecision], N[(c / b), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;b \leq 0.00042:\\
\;\;\;\;\frac{-b}{a}\\

\mathbf{else}:\\
\;\;\;\;\frac{c}{b}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if b < 4.2000000000000002e-4

    1. Initial program 70.4%

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

      \[\leadsto \color{blue}{-1 \cdot \frac{b}{a}} \]
    3. Step-by-step derivation
      1. associate-*r/55.8%

        \[\leadsto \color{blue}{\frac{-1 \cdot b}{a}} \]
      2. mul-1-neg55.8%

        \[\leadsto \frac{\color{blue}{-b}}{a} \]
    4. Simplified55.8%

      \[\leadsto \color{blue}{\frac{-b}{a}} \]

    if 4.2000000000000002e-4 < b

    1. Initial program 14.8%

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

      \[\leadsto \frac{\color{blue}{-2 \cdot b + 2 \cdot \frac{a \cdot c}{b}}}{2 \cdot a} \]
    3. Taylor expanded in b around 0 40.4%

      \[\leadsto \color{blue}{\frac{c}{b}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification50.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq 0.00042:\\ \;\;\;\;\frac{-b}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b}\\ \end{array} \]

Alternative 6: 67.2% accurate, 19.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;b \leq 9.5 \cdot 10^{-250}:\\ \;\;\;\;\frac{-b}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (if (<= b 9.5e-250) (/ (- b) a) (/ (- c) b)))
double code(double a, double b, double c) {
	double tmp;
	if (b <= 9.5e-250) {
		tmp = -b / a;
	} else {
		tmp = -c / b;
	}
	return tmp;
}
real(8) function code(a, b, c)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8), intent (in) :: c
    real(8) :: tmp
    if (b <= 9.5d-250) then
        tmp = -b / a
    else
        tmp = -c / b
    end if
    code = tmp
end function
public static double code(double a, double b, double c) {
	double tmp;
	if (b <= 9.5e-250) {
		tmp = -b / a;
	} else {
		tmp = -c / b;
	}
	return tmp;
}
def code(a, b, c):
	tmp = 0
	if b <= 9.5e-250:
		tmp = -b / a
	else:
		tmp = -c / b
	return tmp
function code(a, b, c)
	tmp = 0.0
	if (b <= 9.5e-250)
		tmp = Float64(Float64(-b) / a);
	else
		tmp = Float64(Float64(-c) / b);
	end
	return tmp
end
function tmp_2 = code(a, b, c)
	tmp = 0.0;
	if (b <= 9.5e-250)
		tmp = -b / a;
	else
		tmp = -c / b;
	end
	tmp_2 = tmp;
end
code[a_, b_, c_] := If[LessEqual[b, 9.5e-250], N[((-b) / a), $MachinePrecision], N[((-c) / b), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;b \leq 9.5 \cdot 10^{-250}:\\
\;\;\;\;\frac{-b}{a}\\

\mathbf{else}:\\
\;\;\;\;\frac{-c}{b}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if b < 9.5000000000000002e-250

    1. Initial program 76.0%

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

      \[\leadsto \color{blue}{-1 \cdot \frac{b}{a}} \]
    3. Step-by-step derivation
      1. associate-*r/66.7%

        \[\leadsto \color{blue}{\frac{-1 \cdot b}{a}} \]
      2. mul-1-neg66.7%

        \[\leadsto \frac{\color{blue}{-b}}{a} \]
    4. Simplified66.7%

      \[\leadsto \color{blue}{\frac{-b}{a}} \]

    if 9.5000000000000002e-250 < b

    1. Initial program 22.5%

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

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}} \]
    3. Step-by-step derivation
      1. associate-*r/81.4%

        \[\leadsto \color{blue}{\frac{-1 \cdot c}{b}} \]
      2. neg-mul-181.4%

        \[\leadsto \frac{\color{blue}{-c}}{b} \]
    4. Simplified81.4%

      \[\leadsto \color{blue}{\frac{-c}{b}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification73.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \leq 9.5 \cdot 10^{-250}:\\ \;\;\;\;\frac{-b}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \]

Alternative 7: 2.5% accurate, 38.7× speedup?

\[\begin{array}{l} \\ \frac{b}{a} \end{array} \]
(FPCore (a b c) :precision binary64 (/ b a))
double code(double a, double b, double c) {
	return b / 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 / a
end function
public static double code(double a, double b, double c) {
	return b / a;
}
def code(a, b, c):
	return b / a
function code(a, b, c)
	return Float64(b / a)
end
function tmp = code(a, b, c)
	tmp = b / a;
end
code[a_, b_, c_] := N[(b / a), $MachinePrecision]
\begin{array}{l}

\\
\frac{b}{a}
\end{array}
Derivation
  1. Initial program 52.8%

    \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
  2. Applied egg-rr32.1%

    \[\leadsto \color{blue}{\frac{0.5}{a} \cdot \left(b + \sqrt{\mathsf{fma}\left(b, b, a \cdot \left(-4 \cdot c\right)\right)}\right)} \]
  3. Taylor expanded in a around 0 2.3%

    \[\leadsto \color{blue}{\frac{b}{a}} \]
  4. Final simplification2.3%

    \[\leadsto \frac{b}{a} \]

Alternative 8: 10.4% accurate, 38.7× 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(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 52.8%

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

    \[\leadsto \frac{\color{blue}{-2 \cdot b + 2 \cdot \frac{a \cdot c}{b}}}{2 \cdot a} \]
  3. Taylor expanded in b around 0 15.0%

    \[\leadsto \color{blue}{\frac{c}{b}} \]
  4. Final simplification15.0%

    \[\leadsto \frac{c}{b} \]

Reproduce

?
herbie shell --seed 2023297 
(FPCore (a b c)
  :name "Quadratic roots, full range"
  :precision binary64
  (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))