
(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 (/ (/ (* 3.0 (* c a)) (* a (+ b (sqrt (fma c (* a -3.0) (* b b)))))) -3.0))
double code(double a, double b, double c) {
return ((3.0 * (c * a)) / (a * (b + sqrt(fma(c, (a * -3.0), (b * b)))))) / -3.0;
}
function code(a, b, c) return Float64(Float64(Float64(3.0 * Float64(c * a)) / Float64(a * Float64(b + sqrt(fma(c, Float64(a * -3.0), Float64(b * b)))))) / -3.0) end
code[a_, b_, c_] := N[(N[(N[(3.0 * N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(a * N[(b + N[Sqrt[N[(c * N[(a * -3.0), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / -3.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{3 \cdot \left(c \cdot a\right)}{a \cdot \left(b + \sqrt{\mathsf{fma}\left(c, a \cdot -3, b \cdot b\right)}\right)}}{-3}
\end{array}
Initial program 55.4%
Applied rewrites55.3%
Applied rewrites57.2%
Taylor expanded in c around 0
lower-*.f64N/A
lower-*.f6499.1
Applied rewrites99.1%
Final simplification99.1%
(FPCore (a b c) :precision binary64 (if (<= b 6.2) (/ (- b (sqrt (fma b b (* a (* c -3.0))))) (* a -3.0)) (fma a (/ (* -0.375 (* c c)) (* b (* b b))) (/ (* c -0.5) b))))
double code(double a, double b, double c) {
double tmp;
if (b <= 6.2) {
tmp = (b - sqrt(fma(b, b, (a * (c * -3.0))))) / (a * -3.0);
} else {
tmp = fma(a, ((-0.375 * (c * c)) / (b * (b * b))), ((c * -0.5) / b));
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 6.2) tmp = Float64(Float64(b - sqrt(fma(b, b, Float64(a * Float64(c * -3.0))))) / Float64(a * -3.0)); else tmp = fma(a, Float64(Float64(-0.375 * Float64(c * c)) / Float64(b * Float64(b * b))), Float64(Float64(c * -0.5) / b)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 6.2], N[(N[(b - N[Sqrt[N[(b * b + N[(a * N[(c * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(a * -3.0), $MachinePrecision]), $MachinePrecision], N[(a * N[(N[(-0.375 * N[(c * c), $MachinePrecision]), $MachinePrecision] / N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(c * -0.5), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 6.2:\\
\;\;\;\;\frac{b - \sqrt{\mathsf{fma}\left(b, b, a \cdot \left(c \cdot -3\right)\right)}}{a \cdot -3}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(a, \frac{-0.375 \cdot \left(c \cdot c\right)}{b \cdot \left(b \cdot b\right)}, \frac{c \cdot -0.5}{b}\right)\\
\end{array}
\end{array}
if b < 6.20000000000000018Initial program 79.5%
Applied rewrites79.4%
lift-/.f64N/A
lift-/.f64N/A
div-invN/A
associate-/l*N/A
lower-*.f64N/A
lift-fma.f64N/A
*-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
associate-*l*N/A
metadata-evalN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f64N/A
Applied rewrites79.5%
lift-*.f64N/A
lift-/.f64N/A
lift-/.f64N/A
associate-/r*N/A
lift-*.f64N/A
un-div-invN/A
lower-/.f6479.5
lift-fma.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f6479.7
Applied rewrites79.7%
if 6.20000000000000018 < b Initial program 49.4%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
associate-*r/N/A
lower-/.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
cube-multN/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6487.1
Applied rewrites87.1%
Final simplification85.6%
(FPCore (a b c) :precision binary64 (if (<= b 6.2) (/ (- b (sqrt (fma b b (* a (* c -3.0))))) (* a -3.0)) (/ (fma (* a -0.375) (* c (/ c (* b b))) (* c -0.5)) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 6.2) {
tmp = (b - sqrt(fma(b, b, (a * (c * -3.0))))) / (a * -3.0);
} else {
tmp = fma((a * -0.375), (c * (c / (b * b))), (c * -0.5)) / b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 6.2) tmp = Float64(Float64(b - sqrt(fma(b, b, Float64(a * Float64(c * -3.0))))) / Float64(a * -3.0)); else tmp = Float64(fma(Float64(a * -0.375), Float64(c * Float64(c / Float64(b * b))), Float64(c * -0.5)) / b); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 6.2], N[(N[(b - N[Sqrt[N[(b * b + N[(a * N[(c * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(a * -3.0), $MachinePrecision]), $MachinePrecision], N[(N[(N[(a * -0.375), $MachinePrecision] * N[(c * N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(c * -0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 6.2:\\
\;\;\;\;\frac{b - \sqrt{\mathsf{fma}\left(b, b, a \cdot \left(c \cdot -3\right)\right)}}{a \cdot -3}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a \cdot -0.375, c \cdot \frac{c}{b \cdot b}, c \cdot -0.5\right)}{b}\\
\end{array}
\end{array}
if b < 6.20000000000000018Initial program 79.5%
Applied rewrites79.4%
lift-/.f64N/A
lift-/.f64N/A
div-invN/A
associate-/l*N/A
lower-*.f64N/A
lift-fma.f64N/A
*-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
associate-*l*N/A
metadata-evalN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f64N/A
Applied rewrites79.5%
lift-*.f64N/A
lift-/.f64N/A
lift-/.f64N/A
associate-/r*N/A
lift-*.f64N/A
un-div-invN/A
lower-/.f6479.5
lift-fma.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f6479.7
Applied rewrites79.7%
if 6.20000000000000018 < b Initial program 49.4%
Taylor expanded in b around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
associate-*r*N/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f6487.0
Applied rewrites87.0%
Final simplification85.6%
(FPCore (a b c) :precision binary64 (if (<= b 6.2) (/ (- b (sqrt (fma b b (* a (* c -3.0))))) (* a -3.0)) (/ (* c (fma -0.375 (* a (/ c (* b b))) -0.5)) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 6.2) {
tmp = (b - sqrt(fma(b, b, (a * (c * -3.0))))) / (a * -3.0);
} else {
tmp = (c * fma(-0.375, (a * (c / (b * b))), -0.5)) / b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 6.2) tmp = Float64(Float64(b - sqrt(fma(b, b, Float64(a * Float64(c * -3.0))))) / Float64(a * -3.0)); else tmp = Float64(Float64(c * fma(-0.375, Float64(a * Float64(c / Float64(b * b))), -0.5)) / b); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 6.2], N[(N[(b - N[Sqrt[N[(b * b + N[(a * N[(c * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(a * -3.0), $MachinePrecision]), $MachinePrecision], N[(N[(c * N[(-0.375 * N[(a * N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 6.2:\\
\;\;\;\;\frac{b - \sqrt{\mathsf{fma}\left(b, b, a \cdot \left(c \cdot -3\right)\right)}}{a \cdot -3}\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot \mathsf{fma}\left(-0.375, a \cdot \frac{c}{b \cdot b}, -0.5\right)}{b}\\
\end{array}
\end{array}
if b < 6.20000000000000018Initial program 79.5%
Applied rewrites79.4%
lift-/.f64N/A
lift-/.f64N/A
div-invN/A
associate-/l*N/A
lower-*.f64N/A
lift-fma.f64N/A
*-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
associate-*l*N/A
metadata-evalN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f64N/A
Applied rewrites79.5%
lift-*.f64N/A
lift-/.f64N/A
lift-/.f64N/A
associate-/r*N/A
lift-*.f64N/A
un-div-invN/A
lower-/.f6479.5
lift-fma.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f6479.7
Applied rewrites79.7%
if 6.20000000000000018 < b Initial program 49.4%
Taylor expanded in b around inf
lower-/.f64N/A
Applied rewrites92.1%
Taylor expanded in c around 0
Applied rewrites86.9%
Final simplification85.5%
(FPCore (a b c) :precision binary64 (if (<= b 6.2) (* (/ (- b (sqrt (fma b b (* a (* c -3.0))))) a) -0.3333333333333333) (/ (* c (fma -0.375 (* a (/ c (* b b))) -0.5)) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 6.2) {
tmp = ((b - sqrt(fma(b, b, (a * (c * -3.0))))) / a) * -0.3333333333333333;
} else {
tmp = (c * fma(-0.375, (a * (c / (b * b))), -0.5)) / b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 6.2) tmp = Float64(Float64(Float64(b - sqrt(fma(b, b, Float64(a * Float64(c * -3.0))))) / a) * -0.3333333333333333); else tmp = Float64(Float64(c * fma(-0.375, Float64(a * Float64(c / Float64(b * b))), -0.5)) / b); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 6.2], N[(N[(N[(b - N[Sqrt[N[(b * b + N[(a * N[(c * -3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / a), $MachinePrecision] * -0.3333333333333333), $MachinePrecision], N[(N[(c * N[(-0.375 * N[(a * N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 6.2:\\
\;\;\;\;\frac{b - \sqrt{\mathsf{fma}\left(b, b, a \cdot \left(c \cdot -3\right)\right)}}{a} \cdot -0.3333333333333333\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot \mathsf{fma}\left(-0.375, a \cdot \frac{c}{b \cdot b}, -0.5\right)}{b}\\
\end{array}
\end{array}
if b < 6.20000000000000018Initial program 79.5%
Applied rewrites79.4%
lift-/.f64N/A
lift-/.f64N/A
div-invN/A
associate-/l*N/A
lower-*.f64N/A
lift-fma.f64N/A
*-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
associate-*l*N/A
metadata-evalN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f64N/A
Applied rewrites79.5%
Applied rewrites79.6%
if 6.20000000000000018 < b Initial program 49.4%
Taylor expanded in b around inf
lower-/.f64N/A
Applied rewrites92.1%
Taylor expanded in c around 0
Applied rewrites86.9%
Final simplification85.5%
(FPCore (a b c) :precision binary64 (if (<= b 6.2) (* (/ -0.3333333333333333 a) (- b (sqrt (fma a (* c -3.0) (* b b))))) (/ (* c (fma -0.375 (* a (/ c (* b b))) -0.5)) b)))
double code(double a, double b, double c) {
double tmp;
if (b <= 6.2) {
tmp = (-0.3333333333333333 / a) * (b - sqrt(fma(a, (c * -3.0), (b * b))));
} else {
tmp = (c * fma(-0.375, (a * (c / (b * b))), -0.5)) / b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 6.2) tmp = Float64(Float64(-0.3333333333333333 / a) * Float64(b - sqrt(fma(a, Float64(c * -3.0), Float64(b * b))))); else tmp = Float64(Float64(c * fma(-0.375, Float64(a * Float64(c / Float64(b * b))), -0.5)) / b); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 6.2], 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[(N[(c * N[(-0.375 * N[(a * N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 6.2:\\
\;\;\;\;\frac{-0.3333333333333333}{a} \cdot \left(b - \sqrt{\mathsf{fma}\left(a, c \cdot -3, b \cdot b\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot \mathsf{fma}\left(-0.375, a \cdot \frac{c}{b \cdot b}, -0.5\right)}{b}\\
\end{array}
\end{array}
if b < 6.20000000000000018Initial program 79.5%
Applied rewrites79.5%
if 6.20000000000000018 < b Initial program 49.4%
Taylor expanded in b around inf
lower-/.f64N/A
Applied rewrites92.1%
Taylor expanded in c around 0
Applied rewrites86.9%
Final simplification85.4%
(FPCore (a b c) :precision binary64 (/ (* c (fma -0.375 (* a (/ c (* b b))) -0.5)) b))
double code(double a, double b, double c) {
return (c * fma(-0.375, (a * (c / (b * b))), -0.5)) / b;
}
function code(a, b, c) return Float64(Float64(c * fma(-0.375, Float64(a * Float64(c / Float64(b * b))), -0.5)) / b) end
code[a_, b_, c_] := N[(N[(c * N[(-0.375 * N[(a * N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot \mathsf{fma}\left(-0.375, a \cdot \frac{c}{b \cdot b}, -0.5\right)}{b}
\end{array}
Initial program 55.4%
Taylor expanded in b around inf
lower-/.f64N/A
Applied rewrites88.1%
Taylor expanded in c around 0
Applied rewrites82.0%
(FPCore (a b c) :precision binary64 (/ (* c -0.5) b))
double code(double a, double b, double c) {
return (c * -0.5) / b;
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = (c * (-0.5d0)) / b
end function
public static double code(double a, double b, double c) {
return (c * -0.5) / b;
}
def code(a, b, c): return (c * -0.5) / b
function code(a, b, c) return Float64(Float64(c * -0.5) / b) end
function tmp = code(a, b, c) tmp = (c * -0.5) / b; end
code[a_, b_, c_] := N[(N[(c * -0.5), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot -0.5}{b}
\end{array}
Initial program 55.4%
Taylor expanded in c around 0
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6464.7
Applied rewrites64.7%
(FPCore (a b c) :precision binary64 (* c (/ -0.5 b)))
double code(double a, double b, double c) {
return c * (-0.5 / b);
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = c * ((-0.5d0) / b)
end function
public static double code(double a, double b, double c) {
return c * (-0.5 / b);
}
def code(a, b, c): return c * (-0.5 / b)
function code(a, b, c) return Float64(c * Float64(-0.5 / b)) end
function tmp = code(a, b, c) tmp = c * (-0.5 / b); end
code[a_, b_, c_] := N[(c * N[(-0.5 / b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \frac{-0.5}{b}
\end{array}
Initial program 55.4%
Taylor expanded in c around 0
associate-*r/N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6464.7
Applied rewrites64.7%
Applied rewrites64.7%
Final simplification64.7%
herbie shell --seed 2024228
(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)))