The quadratic formula (r1)

?

Percentage Accurate: 52.1% → 85.1%
Time: 10.4s
Precision: binary64
Cost: 7624

?

\[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
\[\begin{array}{l} \mathbf{if}\;b \leq -1.35 \cdot 10^{+112}:\\ \;\;\;\;\frac{-b}{a}\\ \mathbf{elif}\;b \leq 3.7 \cdot 10^{-100}:\\ \;\;\;\;\frac{\sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c} - b}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \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.35e+112)
   (/ (- b) a)
   (if (<= b 3.7e-100)
     (/ (- (sqrt (- (* b b) (* (* a 4.0) c))) b) (* a 2.0))
     (/ (- c) b))))
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.35e+112) {
		tmp = -b / a;
	} else if (b <= 3.7e-100) {
		tmp = (sqrt(((b * b) - ((a * 4.0) * c))) - 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
    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.35d+112)) then
        tmp = -b / a
    else if (b <= 3.7d-100) then
        tmp = (sqrt(((b * b) - ((a * 4.0d0) * c))) - b) / (a * 2.0d0)
    else
        tmp = -c / b
    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.35e+112) {
		tmp = -b / a;
	} else if (b <= 3.7e-100) {
		tmp = (Math.sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0);
	} else {
		tmp = -c / b;
	}
	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.35e+112:
		tmp = -b / a
	elif b <= 3.7e-100:
		tmp = (math.sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0)
	else:
		tmp = -c / b
	return tmp
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 code(a, b, c)
	tmp = 0.0
	if (b <= -1.35e+112)
		tmp = Float64(Float64(-b) / a);
	elseif (b <= 3.7e-100)
		tmp = Float64(Float64(sqrt(Float64(Float64(b * b) - Float64(Float64(a * 4.0) * c))) - b) / Float64(a * 2.0));
	else
		tmp = Float64(Float64(-c) / b);
	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.35e+112)
		tmp = -b / a;
	elseif (b <= 3.7e-100)
		tmp = (sqrt(((b * b) - ((a * 4.0) * c))) - b) / (a * 2.0);
	else
		tmp = -c / b;
	end
	tmp_2 = tmp;
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]
code[a_, b_, c_] := If[LessEqual[b, -1.35e+112], N[((-b) / a), $MachinePrecision], If[LessEqual[b, 3.7e-100], N[(N[(N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(a * 4.0), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision], N[((-c) / b), $MachinePrecision]]]
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\begin{array}{l}
\mathbf{if}\;b \leq -1.35 \cdot 10^{+112}:\\
\;\;\;\;\frac{-b}{a}\\

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

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


\end{array}

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.

Herbie found 7 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original52.1%
Target69.9%
Herbie85.1%
\[\begin{array}{l} \mathbf{if}\;b < 0:\\ \;\;\;\;\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{a \cdot \frac{\left(-b\right) - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}}\\ \end{array} \]

Derivation?

  1. Split input into 3 regimes
  2. if b < -1.3500000000000001e112

    1. Initial program 62.7%

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    2. Simplified62.7%

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

      [Start]62.7%

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

      neg-sub0 [=>]62.7%

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

      associate-+l- [=>]62.7%

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

      sub0-neg [=>]62.7%

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

      neg-mul-1 [=>]62.7%

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

      associate-*l/ [<=]62.7%

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

      *-commutative [=>]62.7%

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

      associate-/r* [=>]62.7%

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

      /-rgt-identity [<=]62.7%

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

      metadata-eval [<=]62.7%

      \[ \frac{b - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{--1}} \cdot \frac{\frac{-1}{2}}{a} \]
    3. Taylor expanded in b around -inf 98.4%

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

      \[\leadsto \color{blue}{\frac{-b}{a}} \]
      Step-by-step derivation

      [Start]98.4%

      \[ -1 \cdot \frac{b}{a} \]

      associate-*r/ [=>]98.4%

      \[ \color{blue}{\frac{-1 \cdot b}{a}} \]

      mul-1-neg [=>]98.4%

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

    if -1.3500000000000001e112 < b < 3.70000000000000018e-100

    1. Initial program 81.6%

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

    if 3.70000000000000018e-100 < b

    1. Initial program 9.5%

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
    2. Simplified9.6%

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

      [Start]9.5%

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

      neg-sub0 [=>]9.5%

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

      associate-+l- [=>]9.5%

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

      sub0-neg [=>]9.5%

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

      neg-mul-1 [=>]9.5%

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

      associate-*l/ [<=]9.5%

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

      *-commutative [=>]9.5%

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

      associate-/r* [=>]9.5%

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

      /-rgt-identity [<=]9.5%

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

      metadata-eval [<=]9.5%

      \[ \frac{b - \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{\color{blue}{--1}} \cdot \frac{\frac{-1}{2}}{a} \]
    3. Taylor expanded in b around inf 87.9%

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

      \[\leadsto \color{blue}{\frac{-c}{b}} \]
      Step-by-step derivation

      [Start]87.9%

      \[ -1 \cdot \frac{c}{b} \]

      mul-1-neg [=>]87.9%

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

      distribute-neg-frac [=>]87.9%

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

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

Alternatives

Alternative 1
Accuracy85.1%
Cost7624
\[\begin{array}{l} \mathbf{if}\;b \leq -1.35 \cdot 10^{+112}:\\ \;\;\;\;\frac{-b}{a}\\ \mathbf{elif}\;b \leq 3.7 \cdot 10^{-100}:\\ \;\;\;\;\frac{\sqrt{b \cdot b - \left(a \cdot 4\right) \cdot c} - b}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \]
Alternative 2
Accuracy79.5%
Cost7368
\[\begin{array}{l} \mathbf{if}\;b \leq -64000:\\ \;\;\;\;\frac{c}{b} - \frac{b}{a}\\ \mathbf{elif}\;b \leq 1.2 \cdot 10^{-99}:\\ \;\;\;\;\frac{\sqrt{a \cdot \left(c \cdot -4\right)} - b}{a \cdot 2}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \]
Alternative 3
Accuracy67.0%
Cost580
\[\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 4
Accuracy42.7%
Cost388
\[\begin{array}{l} \mathbf{if}\;b \leq 2.95 \cdot 10^{-16}:\\ \;\;\;\;\frac{-b}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{b}\\ \end{array} \]
Alternative 5
Accuracy66.8%
Cost388
\[\begin{array}{l} \mathbf{if}\;b \leq -5 \cdot 10^{-310}:\\ \;\;\;\;\frac{-b}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{-c}{b}\\ \end{array} \]
Alternative 6
Accuracy2.5%
Cost192
\[\frac{b}{a} \]
Alternative 7
Accuracy10.9%
Cost192
\[\frac{c}{b} \]

Reproduce?

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

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

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