
(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:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
(FPCore (a b c) :precision binary64 (/ -1.0 (* (/ 1.0 c) (+ b (sqrt (fma c (* a -3.0) (* b b)))))))
double code(double a, double b, double c) {
return -1.0 / ((1.0 / c) * (b + sqrt(fma(c, (a * -3.0), (b * b)))));
}
function code(a, b, c) return Float64(-1.0 / Float64(Float64(1.0 / c) * Float64(b + sqrt(fma(c, Float64(a * -3.0), Float64(b * b)))))) end
code[a_, b_, c_] := N[(-1.0 / N[(N[(1.0 / c), $MachinePrecision] * N[(b + N[Sqrt[N[(c * N[(a * -3.0), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{\frac{1}{c} \cdot \left(b + \sqrt{\mathsf{fma}\left(c, a \cdot -3, b \cdot b\right)}\right)}
\end{array}
Initial program 55.4%
Applied rewrites55.4%
Applied rewrites57.2%
Taylor expanded in a around 0
lower-/.f6499.3
Applied rewrites99.3%
(FPCore (a b c) :precision binary64 (if (<= b 15.2) (* (- b (sqrt (fma b b (* c (* a -3.0))))) (/ 1.0 (* a -3.0))) (/ -1.0 (/ (fma -1.5 (/ (* a c) b) (* b 2.0)) c))))
double code(double a, double b, double c) {
double tmp;
if (b <= 15.2) {
tmp = (b - sqrt(fma(b, b, (c * (a * -3.0))))) * (1.0 / (a * -3.0));
} else {
tmp = -1.0 / (fma(-1.5, ((a * c) / b), (b * 2.0)) / c);
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 15.2) tmp = Float64(Float64(b - sqrt(fma(b, b, Float64(c * Float64(a * -3.0))))) * Float64(1.0 / Float64(a * -3.0))); else tmp = Float64(-1.0 / Float64(fma(-1.5, Float64(Float64(a * c) / b), Float64(b * 2.0)) / c)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 15.2], N[(N[(b - N[Sqrt[N[(b * b + N[(c * N[(a * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[(1.0 / N[(a * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-1.0 / N[(N[(-1.5 * N[(N[(a * c), $MachinePrecision] / b), $MachinePrecision] + N[(b * 2.0), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 15.2:\\
\;\;\;\;\left(b - \sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -3\right)\right)}\right) \cdot \frac{1}{a \cdot -3}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\frac{\mathsf{fma}\left(-1.5, \frac{a \cdot c}{b}, b \cdot 2\right)}{c}}\\
\end{array}
\end{array}
if b < 15.199999999999999Initial program 81.4%
Applied rewrites81.4%
lift-/.f64N/A
lift-/.f64N/A
associate-/l/N/A
div-invN/A
metadata-evalN/A
distribute-lft-neg-inN/A
lift-*.f64N/A
lower-*.f64N/A
Applied rewrites81.6%
if 15.199999999999999 < b Initial program 47.4%
Applied rewrites47.4%
Taylor expanded in c around 0
lower-/.f64N/A
lower-fma.f64N/A
lower-/.f64N/A
lower-*.f64N/A
lower-*.f6488.3
Applied rewrites88.3%
Final simplification86.7%
(FPCore (a b c) :precision binary64 (if (<= b 15.2) (/ (- (sqrt (fma b b (* c (* a -3.0)))) b) (* a 3.0)) (/ -1.0 (/ (fma -1.5 (/ (* a c) b) (* b 2.0)) c))))
double code(double a, double b, double c) {
double tmp;
if (b <= 15.2) {
tmp = (sqrt(fma(b, b, (c * (a * -3.0)))) - b) / (a * 3.0);
} else {
tmp = -1.0 / (fma(-1.5, ((a * c) / b), (b * 2.0)) / c);
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 15.2) tmp = Float64(Float64(sqrt(fma(b, b, Float64(c * Float64(a * -3.0)))) - b) / Float64(a * 3.0)); else tmp = Float64(-1.0 / Float64(fma(-1.5, Float64(Float64(a * c) / b), Float64(b * 2.0)) / c)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 15.2], N[(N[(N[Sqrt[N[(b * b + N[(c * N[(a * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision], N[(-1.0 / N[(N[(-1.5 * N[(N[(a * c), $MachinePrecision] / b), $MachinePrecision] + N[(b * 2.0), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 15.2:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -3\right)\right)} - b}{a \cdot 3}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\frac{\mathsf{fma}\left(-1.5, \frac{a \cdot c}{b}, b \cdot 2\right)}{c}}\\
\end{array}
\end{array}
if b < 15.199999999999999Initial program 81.4%
Applied rewrites81.4%
Applied rewrites81.5%
if 15.199999999999999 < b Initial program 47.4%
Applied rewrites47.4%
Taylor expanded in c around 0
lower-/.f64N/A
lower-fma.f64N/A
lower-/.f64N/A
lower-*.f64N/A
lower-*.f6488.3
Applied rewrites88.3%
Final simplification86.7%
(FPCore (a b c) :precision binary64 (if (<= b 15.2) (/ (- (sqrt (fma b b (* c (* a -3.0)))) b) (* a 3.0)) (/ -1.0 (fma 2.0 (/ b c) (* -1.5 (/ a b))))))
double code(double a, double b, double c) {
double tmp;
if (b <= 15.2) {
tmp = (sqrt(fma(b, b, (c * (a * -3.0)))) - b) / (a * 3.0);
} else {
tmp = -1.0 / fma(2.0, (b / c), (-1.5 * (a / b)));
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 15.2) tmp = Float64(Float64(sqrt(fma(b, b, Float64(c * Float64(a * -3.0)))) - b) / Float64(a * 3.0)); else tmp = Float64(-1.0 / fma(2.0, Float64(b / c), Float64(-1.5 * Float64(a / b)))); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 15.2], N[(N[(N[Sqrt[N[(b * b + N[(c * N[(a * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(a * 3.0), $MachinePrecision]), $MachinePrecision], N[(-1.0 / N[(2.0 * N[(b / c), $MachinePrecision] + N[(-1.5 * N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 15.2:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -3\right)\right)} - b}{a \cdot 3}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\mathsf{fma}\left(2, \frac{b}{c}, -1.5 \cdot \frac{a}{b}\right)}\\
\end{array}
\end{array}
if b < 15.199999999999999Initial program 81.4%
Applied rewrites81.4%
Applied rewrites81.5%
if 15.199999999999999 < b Initial program 47.4%
Applied rewrites47.4%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-*.f64N/A
lower-/.f6488.2
Applied rewrites88.2%
(FPCore (a b c) :precision binary64 (if (<= b 15.2) (/ (* (- (sqrt (fma b b (* c (* a -3.0)))) b) 0.3333333333333333) a) (/ -1.0 (fma 2.0 (/ b c) (* -1.5 (/ a b))))))
double code(double a, double b, double c) {
double tmp;
if (b <= 15.2) {
tmp = ((sqrt(fma(b, b, (c * (a * -3.0)))) - b) * 0.3333333333333333) / a;
} else {
tmp = -1.0 / fma(2.0, (b / c), (-1.5 * (a / b)));
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 15.2) tmp = Float64(Float64(Float64(sqrt(fma(b, b, Float64(c * Float64(a * -3.0)))) - b) * 0.3333333333333333) / a); else tmp = Float64(-1.0 / fma(2.0, Float64(b / c), Float64(-1.5 * Float64(a / b)))); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 15.2], N[(N[(N[(N[Sqrt[N[(b * b + N[(c * N[(a * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] * 0.3333333333333333), $MachinePrecision] / a), $MachinePrecision], N[(-1.0 / N[(2.0 * N[(b / c), $MachinePrecision] + N[(-1.5 * N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 15.2:\\
\;\;\;\;\frac{\left(\sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -3\right)\right)} - b\right) \cdot 0.3333333333333333}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\mathsf{fma}\left(2, \frac{b}{c}, -1.5 \cdot \frac{a}{b}\right)}\\
\end{array}
\end{array}
if b < 15.199999999999999Initial program 81.4%
Applied rewrites81.4%
Applied rewrites81.5%
if 15.199999999999999 < b Initial program 47.4%
Applied rewrites47.4%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-*.f64N/A
lower-/.f6488.2
Applied rewrites88.2%
(FPCore (a b c) :precision binary64 (if (<= b 15.2) (* 0.3333333333333333 (/ (- (sqrt (fma b b (* c (* a -3.0)))) b) a)) (/ -1.0 (fma 2.0 (/ b c) (* -1.5 (/ a b))))))
double code(double a, double b, double c) {
double tmp;
if (b <= 15.2) {
tmp = 0.3333333333333333 * ((sqrt(fma(b, b, (c * (a * -3.0)))) - b) / a);
} else {
tmp = -1.0 / fma(2.0, (b / c), (-1.5 * (a / b)));
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 15.2) tmp = Float64(0.3333333333333333 * Float64(Float64(sqrt(fma(b, b, Float64(c * Float64(a * -3.0)))) - b) / a)); else tmp = Float64(-1.0 / fma(2.0, Float64(b / c), Float64(-1.5 * Float64(a / b)))); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 15.2], N[(0.3333333333333333 * N[(N[(N[Sqrt[N[(b * b + N[(c * N[(a * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision], N[(-1.0 / N[(2.0 * N[(b / c), $MachinePrecision] + N[(-1.5 * N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 15.2:\\
\;\;\;\;0.3333333333333333 \cdot \frac{\sqrt{\mathsf{fma}\left(b, b, c \cdot \left(a \cdot -3\right)\right)} - b}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\mathsf{fma}\left(2, \frac{b}{c}, -1.5 \cdot \frac{a}{b}\right)}\\
\end{array}
\end{array}
if b < 15.199999999999999Initial program 81.4%
Applied rewrites81.4%
Applied rewrites81.5%
if 15.199999999999999 < b Initial program 47.4%
Applied rewrites47.4%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-*.f64N/A
lower-/.f6488.2
Applied rewrites88.2%
Final simplification86.7%
(FPCore (a b c) :precision binary64 (if (<= b 10.5) (* (/ -0.3333333333333333 a) (- b (sqrt (fma a (* c -3.0) (* b b))))) (/ -1.0 (fma 2.0 (/ b c) (* -1.5 (/ a b))))))
double code(double a, double b, double c) {
double tmp;
if (b <= 10.5) {
tmp = (-0.3333333333333333 / a) * (b - sqrt(fma(a, (c * -3.0), (b * b))));
} else {
tmp = -1.0 / fma(2.0, (b / c), (-1.5 * (a / b)));
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 10.5) tmp = Float64(Float64(-0.3333333333333333 / a) * Float64(b - sqrt(fma(a, Float64(c * -3.0), Float64(b * b))))); else tmp = Float64(-1.0 / fma(2.0, Float64(b / c), Float64(-1.5 * Float64(a / b)))); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 10.5], N[(N[(-0.3333333333333333 / a), $MachinePrecision] * N[(b - N[Sqrt[N[(a * N[(c * -3.0), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-1.0 / N[(2.0 * N[(b / c), $MachinePrecision] + N[(-1.5 * N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 10.5:\\
\;\;\;\;\frac{-0.3333333333333333}{a} \cdot \left(b - \sqrt{\mathsf{fma}\left(a, c \cdot -3, b \cdot b\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{-1}{\mathsf{fma}\left(2, \frac{b}{c}, -1.5 \cdot \frac{a}{b}\right)}\\
\end{array}
\end{array}
if b < 10.5Initial program 81.7%
Applied rewrites81.7%
if 10.5 < b Initial program 47.7%
Applied rewrites47.7%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-*.f64N/A
lower-/.f6488.1
Applied rewrites88.1%
Final simplification86.6%
(FPCore (a b c) :precision binary64 (/ -1.0 (fma 2.0 (/ b c) (* -1.5 (/ a b)))))
double code(double a, double b, double c) {
return -1.0 / fma(2.0, (b / c), (-1.5 * (a / b)));
}
function code(a, b, c) return Float64(-1.0 / fma(2.0, Float64(b / c), Float64(-1.5 * Float64(a / b)))) end
code[a_, b_, c_] := N[(-1.0 / N[(2.0 * N[(b / c), $MachinePrecision] + N[(-1.5 * N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{\mathsf{fma}\left(2, \frac{b}{c}, -1.5 \cdot \frac{a}{b}\right)}
\end{array}
Initial program 55.4%
Applied rewrites55.4%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
lower-*.f64N/A
lower-/.f6481.5
Applied rewrites81.5%
(FPCore (a b c) :precision binary64 (* -0.5 (/ c b)))
double code(double a, double b, double c) {
return -0.5 * (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 = (-0.5d0) * (c / b)
end function
public static double code(double a, double b, double c) {
return -0.5 * (c / b);
}
def code(a, b, c): return -0.5 * (c / b)
function code(a, b, c) return Float64(-0.5 * Float64(c / b)) end
function tmp = code(a, b, c) tmp = -0.5 * (c / b); end
code[a_, b_, c_] := N[(-0.5 * N[(c / b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
-0.5 \cdot \frac{c}{b}
\end{array}
Initial program 55.4%
Taylor expanded in b around inf
lower-*.f64N/A
lower-/.f6464.1
Applied rewrites64.1%
herbie shell --seed 2024221
(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)))