Cubic critical, narrow range

Percentage Accurate: 55.5% → 99.3%
Time: 14.8s
Alternatives: 10
Speedup: 2.9×

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(3 \cdot a\right) \cdot c}}{3 \cdot a} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))
double code(double a, double b, double c) {
	return (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (3.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) - ((3.0d0 * a) * c)))) / (3.0d0 * a)
end function
public static double code(double a, double b, double c) {
	return (-b + Math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a);
}
def code(a, b, c):
	return (-b + math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a)
function code(a, b, c)
	return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(3.0 * a) * c)))) / Float64(3.0 * a))
end
function tmp = code(a, b, c)
	tmp = (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a);
end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(3.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(3.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \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 10 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.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))
double code(double a, double b, double c) {
	return (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (3.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) - ((3.0d0 * a) * c)))) / (3.0d0 * a)
end function
public static double code(double a, double b, double c) {
	return (-b + Math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a);
}
def code(a, b, c):
	return (-b + math.sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a)
function code(a, b, c)
	return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(3.0 * a) * c)))) / Float64(3.0 * a))
end
function tmp = code(a, b, c)
	tmp = (-b + sqrt(((b * b) - ((3.0 * a) * c)))) / (3.0 * a);
end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(3.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(3.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Alternative 1: 99.3% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := c \cdot \left(a \cdot -3\right)\\ \frac{t\_0}{\left(b + \sqrt{\mathsf{fma}\left(b, b, t\_0\right)}\right) \cdot \left(a \cdot 3\right)} \end{array} \end{array} \]
(FPCore (a b c)
 :precision binary64
 (let* ((t_0 (* c (* a -3.0))))
   (/ t_0 (* (+ b (sqrt (fma b b t_0))) (* a 3.0)))))
double code(double a, double b, double c) {
	double t_0 = c * (a * -3.0);
	return t_0 / ((b + sqrt(fma(b, b, t_0))) * (a * 3.0));
}
function code(a, b, c)
	t_0 = Float64(c * Float64(a * -3.0))
	return Float64(t_0 / Float64(Float64(b + sqrt(fma(b, b, t_0))) * Float64(a * 3.0)))
end
code[a_, b_, c_] := Block[{t$95$0 = N[(c * N[(a * -3.0), $MachinePrecision]), $MachinePrecision]}, N[(t$95$0 / N[(N[(b + N[Sqrt[N[(b * b + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[(a * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := c \cdot \left(a \cdot -3\right)\\
\frac{t\_0}{\left(b + \sqrt{\mathsf{fma}\left(b, b, t\_0\right)}\right) \cdot \left(a \cdot 3\right)}
\end{array}
\end{array}
Derivation
  1. Initial program 53.8%

    \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a} \]
  2. Add Preprocessing
  3. Applied rewrites53.8%

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \frac{c \cdot \left(a \cdot -3\right)}{\left(b + \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -3\right)\right)}\right) \cdot \left(a \cdot 3\right)} \]
  9. Add Preprocessing

Alternative 2: 85.1% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\frac{\sqrt{b \cdot b - c \cdot \left(a \cdot 3\right)} - b}{a \cdot 3} \leq -0.004:\\
\;\;\;\;\frac{\sqrt{c \cdot \left(a \cdot -3\right) + b \cdot b} - b}{a \cdot 3}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 #s(literal 3 binary64) a) c)))) (*.f64 #s(literal 3 binary64) a)) < -0.0040000000000000001

    1. Initial program 77.3%

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a} \]
    2. Add Preprocessing
    3. Applied rewrites77.3%

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

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

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

    if -0.0040000000000000001 < (/.f64 (+.f64 (neg.f64 b) (sqrt.f64 (-.f64 (*.f64 b b) (*.f64 (*.f64 #s(literal 3 binary64) a) c)))) (*.f64 #s(literal 3 binary64) a))

    1. Initial program 46.1%

      \[\frac{\left(-b\right) + \sqrt{b \cdot b - \left(3 \cdot a\right) \cdot c}}{3 \cdot a} \]
    2. Add Preprocessing
    3. Applied rewrites46.1%

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\frac{-1}{2} \cdot \frac{c}{b} + \frac{-3}{8} \cdot \frac{a \cdot {c}^{2}}{{b}^{3}}} \]
    7. Step-by-step derivation
      1. lower-fma.f64N/A

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

        \[\leadsto \mathsf{fma}\left(\frac{-1}{2}, \color{blue}{\frac{c}{b}}, \frac{-3}{8} \cdot \frac{a \cdot {c}^{2}}{{b}^{3}}\right) \]
      3. associate-*r/N/A

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

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

        \[\leadsto \mathsf{fma}\left(\frac{-1}{2}, \frac{c}{b}, \frac{\color{blue}{\frac{-3}{8} \cdot \left(a \cdot {c}^{2}\right)}}{{b}^{3}}\right) \]
      6. lower-*.f64N/A

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

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

        \[\leadsto \mathsf{fma}\left(\frac{-1}{2}, \frac{c}{b}, \frac{\frac{-3}{8} \cdot \left(a \cdot \color{blue}{\left(c \cdot c\right)}\right)}{{b}^{3}}\right) \]
      9. cube-multN/A

        \[\leadsto \mathsf{fma}\left(\frac{-1}{2}, \frac{c}{b}, \frac{\frac{-3}{8} \cdot \left(a \cdot \left(c \cdot c\right)\right)}{\color{blue}{b \cdot \left(b \cdot b\right)}}\right) \]
      10. unpow2N/A

        \[\leadsto \mathsf{fma}\left(\frac{-1}{2}, \frac{c}{b}, \frac{\frac{-3}{8} \cdot \left(a \cdot \left(c \cdot c\right)\right)}{b \cdot \color{blue}{{b}^{2}}}\right) \]
      11. lower-*.f64N/A

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

        \[\leadsto \mathsf{fma}\left(\frac{-1}{2}, \frac{c}{b}, \frac{\frac{-3}{8} \cdot \left(a \cdot \left(c \cdot c\right)\right)}{b \cdot \color{blue}{\left(b \cdot b\right)}}\right) \]
      13. lower-*.f6488.5

        \[\leadsto \mathsf{fma}\left(-0.5, \frac{c}{b}, \frac{-0.375 \cdot \left(a \cdot \left(c \cdot c\right)\right)}{b \cdot \color{blue}{\left(b \cdot b\right)}}\right) \]
    8. Applied rewrites88.5%

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

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

Reproduce

?
herbie shell --seed 2024226 
(FPCore (a b c)
  :name "Cubic critical, 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) (* (* 3.0 a) c)))) (* 3.0 a)))