Average Error: 34.6 → 10.8
Time: 29.6s
Precision: binary64
Cost: 7688
\[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \]
\[\begin{array}{l} \mathbf{if}\;b \leq -1.9 \cdot 10^{-121}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \leq 2.2 \cdot 10^{+141}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;-\frac{b}{a}\\ \end{array} \]
(FPCore (a b c)
 :precision binary64
 (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))
(FPCore (a b c)
 :precision binary64
 (if (<= b -1.9e-121)
   (- (/ c b))
   (if (<= b 2.2e+141)
     (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a))
     (- (/ b a)))))
double code(double a, double b, double c) {
	return (-b - sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a);
}
double code(double a, double b, double c) {
	double tmp;
	if (b <= -1.9e-121) {
		tmp = -(c / b);
	} else if (b <= 2.2e+141) {
		tmp = (-b - sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a);
	} else {
		tmp = -(b / a);
	}
	return tmp;
}
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
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 <= (-1.9d-121)) then
        tmp = -(c / b)
    else if (b <= 2.2d+141) then
        tmp = (-b - sqrt(((b * b) - (4.0d0 * (a * c))))) / (2.0d0 * a)
    else
        tmp = -(b / a)
    end if
    code = tmp
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);
}
public static double code(double a, double b, double c) {
	double tmp;
	if (b <= -1.9e-121) {
		tmp = -(c / b);
	} else if (b <= 2.2e+141) {
		tmp = (-b - Math.sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a);
	} else {
		tmp = -(b / a);
	}
	return tmp;
}
def code(a, b, c):
	return (-b - math.sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a)
def code(a, b, c):
	tmp = 0
	if b <= -1.9e-121:
		tmp = -(c / b)
	elif b <= 2.2e+141:
		tmp = (-b - math.sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a)
	else:
		tmp = -(b / a)
	return tmp
function code(a, b, c)
	return Float64(Float64(Float64(-b) - sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(a * c))))) / Float64(2.0 * a))
end
function code(a, b, c)
	tmp = 0.0
	if (b <= -1.9e-121)
		tmp = Float64(-Float64(c / b));
	elseif (b <= 2.2e+141)
		tmp = Float64(Float64(Float64(-b) - sqrt(Float64(Float64(b * b) - Float64(4.0 * Float64(a * c))))) / Float64(2.0 * a));
	else
		tmp = Float64(-Float64(b / a));
	end
	return tmp
end
function tmp = code(a, b, c)
	tmp = (-b - sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a);
end
function tmp_2 = code(a, b, c)
	tmp = 0.0;
	if (b <= -1.9e-121)
		tmp = -(c / b);
	elseif (b <= 2.2e+141)
		tmp = (-b - sqrt(((b * b) - (4.0 * (a * c))))) / (2.0 * a);
	else
		tmp = -(b / a);
	end
	tmp_2 = tmp;
end
code[a_, b_, c_] := N[(N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
code[a_, b_, c_] := If[LessEqual[b, -1.9e-121], (-N[(c / b), $MachinePrecision]), If[LessEqual[b, 2.2e+141], N[(N[((-b) - N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], (-N[(b / a), $MachinePrecision])]]
\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}
\begin{array}{l}
\mathbf{if}\;b \leq -1.9 \cdot 10^{-121}:\\
\;\;\;\;-\frac{c}{b}\\

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

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


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original34.6
Target21.1
Herbie10.8
\[\begin{array}{l} \mathbf{if}\;b < 0:\\ \;\;\;\;\frac{c}{a \cdot \frac{\left(-b\right) + \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a}\\ \end{array} \]

Derivation

  1. Split input into 3 regimes
  2. if b < -1.9e-121

    1. Initial program 51.1

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \]
    2. Simplified51.1

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

      \[\leadsto \color{blue}{-1 \cdot \frac{c}{b}} \]
    4. Simplified11.6

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

    if -1.9e-121 < b < 2.2e141

    1. Initial program 12.2

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

    if 2.2e141 < b

    1. Initial program 59.2

      \[\frac{\left(-b\right) - \sqrt{b \cdot b - 4 \cdot \left(a \cdot c\right)}}{2 \cdot a} \]
    2. Simplified59.2

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

      \[\leadsto \color{blue}{-1 \cdot \frac{b}{a}} \]
    4. Simplified2.1

      \[\leadsto \color{blue}{-\frac{b}{a}} \]
      Proof
  3. Recombined 3 regimes into one program.

Alternatives

Alternative 1
Error14.7
Cost7632
\[\begin{array}{l} \mathbf{if}\;b \leq -4.2 \cdot 10^{-117}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \leq 2.2 \cdot 10^{-105}:\\ \;\;\;\;\frac{-\sqrt{-4 \cdot \left(c \cdot a\right)}}{a + a}\\ \mathbf{elif}\;b \leq 1.8 \cdot 10^{-46}:\\ \;\;\;\;-\frac{b}{a}\\ \mathbf{elif}\;b \leq 1.18 \cdot 10^{-21}:\\ \;\;\;\;\left(\sqrt{\left(c \cdot a\right) \cdot -4} + b\right) \cdot \frac{-0.5}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \end{array} \]
Alternative 2
Error14.3
Cost7632
\[\begin{array}{l} \mathbf{if}\;b \leq -5 \cdot 10^{-117}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \leq 2.1 \cdot 10^{-69}:\\ \;\;\;\;\frac{\left(-b\right) - \sqrt{-4 \cdot \left(c \cdot a\right)}}{2 \cdot a}\\ \mathbf{elif}\;b \leq 1.15 \cdot 10^{-49}:\\ \;\;\;\;-\frac{b}{a}\\ \mathbf{elif}\;b \leq 2.1 \cdot 10^{-22}:\\ \;\;\;\;\left(\sqrt{\left(c \cdot a\right) \cdot -4} + b\right) \cdot \frac{-0.5}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \end{array} \]
Alternative 3
Error10.8
Cost7624
\[\begin{array}{l} \mathbf{if}\;b \leq -5 \cdot 10^{-117}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \leq 2.25 \cdot 10^{+141}:\\ \;\;\;\;\left(b + \sqrt{\left(-4 \cdot c\right) \cdot a + b \cdot b}\right) \cdot \frac{-0.5}{a}\\ \mathbf{else}:\\ \;\;\;\;-\frac{b}{a}\\ \end{array} \]
Alternative 4
Error14.3
Cost7304
\[\begin{array}{l} \mathbf{if}\;b \leq -4.2 \cdot 10^{-117}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{elif}\;b \leq 2.2 \cdot 10^{-105}:\\ \;\;\;\;\frac{-\sqrt{-4 \cdot \left(c \cdot a\right)}}{a + a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \end{array} \]
Alternative 5
Error22.5
Cost580
\[\begin{array}{l} \mathbf{if}\;b \leq -3.7 \cdot 10^{-295}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \end{array} \]
Alternative 6
Error39.6
Cost388
\[\begin{array}{l} \mathbf{if}\;b \leq -3.8 \cdot 10^{-21}:\\ \;\;\;\;\frac{c}{b}\\ \mathbf{else}:\\ \;\;\;\;-\frac{b}{a}\\ \end{array} \]
Alternative 7
Error22.6
Cost388
\[\begin{array}{l} \mathbf{if}\;b \leq -4.7 \cdot 10^{-229}:\\ \;\;\;\;-\frac{c}{b}\\ \mathbf{else}:\\ \;\;\;\;-\frac{b}{a}\\ \end{array} \]
Alternative 8
Error56.4
Cost192
\[\frac{c}{b} \]

Error

Reproduce

herbie shell --seed 2023010 
(FPCore (a b c)
  :name "The quadratic formula (r2)"
  :precision binary64

  :herbie-target
  (if (< b 0.0) (/ c (* a (/ (+ (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))) (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))

  (/ (- (- b) (sqrt (- (* b b) (* 4.0 (* a c))))) (* 2.0 a)))