Quadratic roots, narrow range

?

Percentage Accurate: 55.5% → 91.1%
Time: 13.9s
Precision: binary64
Cost: 55168

?

\[\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)\]
\[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a} \]
\[\begin{array}{l} \\ \begin{array}{l} t_0 := {\left(a \cdot c\right)}^{4}\\ \frac{1}{\frac{a}{b} + \mathsf{fma}\left(-2, \frac{\left(a \cdot a\right) \cdot \left(c \cdot -0.5\right)}{{b}^{3}}, \frac{-2 \cdot \left(\mathsf{fma}\left({a}^{3}, c \cdot c, \frac{-0.125}{a} \cdot \frac{\mathsf{fma}\left(16, t_0, 4 \cdot t_0\right)}{c \cdot c}\right) - c \cdot \left(\left(a \cdot \left(a \cdot c\right)\right) \cdot \left(a \cdot -0.5\right)\right)\right)}{{b}^{5}} - \frac{b}{c}\right)} \end{array} \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
 (let* ((t_0 (pow (* a c) 4.0)))
   (/
    1.0
    (+
     (/ a b)
     (fma
      -2.0
      (/ (* (* a a) (* c -0.5)) (pow b 3.0))
      (-
       (/
        (*
         -2.0
         (-
          (fma
           (pow a 3.0)
           (* c c)
           (* (/ -0.125 a) (/ (fma 16.0 t_0 (* 4.0 t_0)) (* c c))))
          (* c (* (* a (* a c)) (* a -0.5)))))
        (pow b 5.0))
       (/ b c)))))))
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 t_0 = pow((a * c), 4.0);
	return 1.0 / ((a / b) + fma(-2.0, (((a * a) * (c * -0.5)) / pow(b, 3.0)), (((-2.0 * (fma(pow(a, 3.0), (c * c), ((-0.125 / a) * (fma(16.0, t_0, (4.0 * t_0)) / (c * c)))) - (c * ((a * (a * c)) * (a * -0.5))))) / pow(b, 5.0)) - (b / c))));
}
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)
	t_0 = Float64(a * c) ^ 4.0
	return Float64(1.0 / Float64(Float64(a / b) + fma(-2.0, Float64(Float64(Float64(a * a) * Float64(c * -0.5)) / (b ^ 3.0)), Float64(Float64(Float64(-2.0 * Float64(fma((a ^ 3.0), Float64(c * c), Float64(Float64(-0.125 / a) * Float64(fma(16.0, t_0, Float64(4.0 * t_0)) / Float64(c * c)))) - Float64(c * Float64(Float64(a * Float64(a * c)) * Float64(a * -0.5))))) / (b ^ 5.0)) - Float64(b / c)))))
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_] := Block[{t$95$0 = N[Power[N[(a * c), $MachinePrecision], 4.0], $MachinePrecision]}, N[(1.0 / N[(N[(a / b), $MachinePrecision] + N[(-2.0 * N[(N[(N[(a * a), $MachinePrecision] * N[(c * -0.5), $MachinePrecision]), $MachinePrecision] / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[(N[(-2.0 * N[(N[(N[Power[a, 3.0], $MachinePrecision] * N[(c * c), $MachinePrecision] + N[(N[(-0.125 / a), $MachinePrecision] * N[(N[(16.0 * t$95$0 + N[(4.0 * t$95$0), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(c * N[(N[(a * N[(a * c), $MachinePrecision]), $MachinePrecision] * N[(a * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Power[b, 5.0], $MachinePrecision]), $MachinePrecision] - N[(b / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\begin{array}{l}

\\
\begin{array}{l}
t_0 := {\left(a \cdot c\right)}^{4}\\
\frac{1}{\frac{a}{b} + \mathsf{fma}\left(-2, \frac{\left(a \cdot a\right) \cdot \left(c \cdot -0.5\right)}{{b}^{3}}, \frac{-2 \cdot \left(\mathsf{fma}\left({a}^{3}, c \cdot c, \frac{-0.125}{a} \cdot \frac{\mathsf{fma}\left(16, t_0, 4 \cdot t_0\right)}{c \cdot c}\right) - c \cdot \left(\left(a \cdot \left(a \cdot c\right)\right) \cdot \left(a \cdot -0.5\right)\right)\right)}{{b}^{5}} - \frac{b}{c}\right)}
\end{array}
\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

Derivation?

  1. Initial program 54.5%

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

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

    [Start]54.5%

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

    add-cbrt-cube [=>]54.5%

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

    pow3 [=>]54.5%

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

    neg-mul-1 [=>]54.5%

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

    fma-def [=>]54.5%

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

    *-commutative [=>]54.5%

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

    *-commutative [=>]54.5%

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

    *-commutative [=>]54.5%

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

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

    [Start]54.5%

    \[ \sqrt[3]{{\left(\frac{\mathsf{fma}\left(-1, b, \sqrt{b \cdot b - c \cdot \left(a \cdot 4\right)}\right)}{a \cdot 2}\right)}^{3}} \]

    rem-cbrt-cube [=>]54.5%

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

    clear-num [=>]54.5%

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

    \[\leadsto \frac{1}{\color{blue}{\frac{a}{b} + \left(-2 \cdot \frac{0.5 \cdot \left(c \cdot {a}^{2}\right) + -1 \cdot \left(c \cdot {a}^{2}\right)}{{b}^{3}} + \left(-1 \cdot \frac{b}{c} + -2 \cdot \frac{-1 \cdot \left(c \cdot \left(\left(0.5 \cdot \left(c \cdot {a}^{2}\right) + -1 \cdot \left(c \cdot {a}^{2}\right)\right) \cdot a\right)\right) + \left({c}^{2} \cdot {a}^{3} + -0.125 \cdot \frac{{\left(-2 \cdot \left({c}^{2} \cdot {a}^{2}\right)\right)}^{2} + 16 \cdot \left({c}^{4} \cdot {a}^{4}\right)}{{c}^{2} \cdot a}\right)}{{b}^{5}}\right)\right)}} \]
  5. Simplified92.5%

    \[\leadsto \frac{1}{\color{blue}{\frac{a}{b} + \mathsf{fma}\left(-2, \frac{\left(a \cdot a\right) \cdot \left(c \cdot -0.5\right)}{{b}^{3}}, \frac{-2 \cdot \left(\mathsf{fma}\left({a}^{3}, c \cdot c, \frac{-0.125}{a} \cdot \frac{\mathsf{fma}\left(16, {\left(c \cdot a\right)}^{4}, 4 \cdot {\left(c \cdot a\right)}^{4}\right)}{c \cdot c}\right) - c \cdot \left(\left(a \cdot \left(c \cdot a\right)\right) \cdot \left(-0.5 \cdot a\right)\right)\right)}{{b}^{5}} - \frac{b}{c}\right)}} \]
    Step-by-step derivation

    [Start]92.5%

    \[ \frac{1}{\frac{a}{b} + \left(-2 \cdot \frac{0.5 \cdot \left(c \cdot {a}^{2}\right) + -1 \cdot \left(c \cdot {a}^{2}\right)}{{b}^{3}} + \left(-1 \cdot \frac{b}{c} + -2 \cdot \frac{-1 \cdot \left(c \cdot \left(\left(0.5 \cdot \left(c \cdot {a}^{2}\right) + -1 \cdot \left(c \cdot {a}^{2}\right)\right) \cdot a\right)\right) + \left({c}^{2} \cdot {a}^{3} + -0.125 \cdot \frac{{\left(-2 \cdot \left({c}^{2} \cdot {a}^{2}\right)\right)}^{2} + 16 \cdot \left({c}^{4} \cdot {a}^{4}\right)}{{c}^{2} \cdot a}\right)}{{b}^{5}}\right)\right)} \]
  6. Final simplification92.5%

    \[\leadsto \frac{1}{\frac{a}{b} + \mathsf{fma}\left(-2, \frac{\left(a \cdot a\right) \cdot \left(c \cdot -0.5\right)}{{b}^{3}}, \frac{-2 \cdot \left(\mathsf{fma}\left({a}^{3}, c \cdot c, \frac{-0.125}{a} \cdot \frac{\mathsf{fma}\left(16, {\left(a \cdot c\right)}^{4}, 4 \cdot {\left(a \cdot c\right)}^{4}\right)}{c \cdot c}\right) - c \cdot \left(\left(a \cdot \left(a \cdot c\right)\right) \cdot \left(a \cdot -0.5\right)\right)\right)}{{b}^{5}} - \frac{b}{c}\right)} \]

Alternatives

Alternative 1
Accuracy91.1%
Cost55168
\[\begin{array}{l} t_0 := {\left(a \cdot c\right)}^{4}\\ \frac{1}{\frac{a}{b} + \mathsf{fma}\left(-2, \frac{\left(a \cdot a\right) \cdot \left(c \cdot -0.5\right)}{{b}^{3}}, \frac{-2 \cdot \left(\mathsf{fma}\left({a}^{3}, c \cdot c, \frac{-0.125}{a} \cdot \frac{\mathsf{fma}\left(16, t_0, 4 \cdot t_0\right)}{c \cdot c}\right) - c \cdot \left(\left(a \cdot \left(a \cdot c\right)\right) \cdot \left(a \cdot -0.5\right)\right)\right)}{{b}^{5}} - \frac{b}{c}\right)} \end{array} \]
Alternative 2
Accuracy90.8%
Cost40704
\[\mathsf{fma}\left(-0.25, \frac{{\left(a \cdot c\right)}^{4}}{a} \cdot \frac{20}{{b}^{7}}, -2 \cdot \frac{{c}^{3}}{\frac{{b}^{5}}{a \cdot a}} - \frac{c}{b}\right) - \frac{c \cdot c}{\frac{{b}^{3}}{a}} \]
Alternative 3
Accuracy88.2%
Cost7808
\[\frac{1}{\left(\frac{a}{b} - \frac{b}{c}\right) + \frac{-2 \cdot \left(\left(a \cdot a\right) \cdot \left(c \cdot -0.5\right)\right)}{{b}^{3}}} \]
Alternative 4
Accuracy85.4%
Cost7492
\[\begin{array}{l} \mathbf{if}\;b \leq 2.3:\\ \;\;\;\;\left(\sqrt{b \cdot b + \left(a \cdot c\right) \cdot -4} - b\right) \cdot \frac{0.5}{a}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{a}{b} - \frac{b}{c}}\\ \end{array} \]
Alternative 5
Accuracy82.1%
Cost576
\[\frac{1}{\frac{a}{b} - \frac{b}{c}} \]
Alternative 6
Accuracy64.3%
Cost256
\[\frac{-c}{b} \]
Alternative 7
Accuracy3.2%
Cost192
\[\frac{0}{a} \]

Reproduce?

herbie shell --seed 2023167 
(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)))