
(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 7 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 (/ (/ (fma a (* c 3.0) 0.0) (* a (+ b (sqrt (fma -3.0 (* a c) (* b b)))))) -3.0))
double code(double a, double b, double c) {
return (fma(a, (c * 3.0), 0.0) / (a * (b + sqrt(fma(-3.0, (a * c), (b * b)))))) / -3.0;
}
function code(a, b, c) return Float64(Float64(fma(a, Float64(c * 3.0), 0.0) / Float64(a * Float64(b + sqrt(fma(-3.0, Float64(a * c), Float64(b * b)))))) / -3.0) end
code[a_, b_, c_] := N[(N[(N[(a * N[(c * 3.0), $MachinePrecision] + 0.0), $MachinePrecision] / N[(a * N[(b + N[Sqrt[N[(-3.0 * N[(a * c), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / -3.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\mathsf{fma}\left(a, c \cdot 3, 0\right)}{a \cdot \left(b + \sqrt{\mathsf{fma}\left(-3, a \cdot c, b \cdot b\right)}\right)}}{-3}
\end{array}
Initial program 32.7%
Applied rewrites32.7%
lift-*.f64N/A
lift-/.f64N/A
un-div-invN/A
lift--.f64N/A
flip--N/A
associate-/l/N/A
lower-/.f64N/A
Applied rewrites99.2%
(FPCore (a b c) :precision binary64 (/ (* (fma a (* c 3.0) 0.0) (/ -0.3333333333333333 a)) (+ b (sqrt (fma -3.0 (* a c) (* b b))))))
double code(double a, double b, double c) {
return (fma(a, (c * 3.0), 0.0) * (-0.3333333333333333 / a)) / (b + sqrt(fma(-3.0, (a * c), (b * b))));
}
function code(a, b, c) return Float64(Float64(fma(a, Float64(c * 3.0), 0.0) * Float64(-0.3333333333333333 / a)) / Float64(b + sqrt(fma(-3.0, Float64(a * c), Float64(b * b))))) end
code[a_, b_, c_] := N[(N[(N[(a * N[(c * 3.0), $MachinePrecision] + 0.0), $MachinePrecision] * N[(-0.3333333333333333 / a), $MachinePrecision]), $MachinePrecision] / N[(b + N[Sqrt[N[(-3.0 * N[(a * c), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(a, c \cdot 3, 0\right) \cdot \frac{-0.3333333333333333}{a}}{b + \sqrt{\mathsf{fma}\left(-3, a \cdot c, b \cdot b\right)}}
\end{array}
Initial program 32.7%
Applied rewrites32.7%
Applied rewrites99.2%
(FPCore (a b c) :precision binary64 (/ (fma a (* c 3.0) 0.0) (* (* a 3.0) (- (- b) (sqrt (fma -3.0 (* a c) (* b b)))))))
double code(double a, double b, double c) {
return fma(a, (c * 3.0), 0.0) / ((a * 3.0) * (-b - sqrt(fma(-3.0, (a * c), (b * b)))));
}
function code(a, b, c) return Float64(fma(a, Float64(c * 3.0), 0.0) / Float64(Float64(a * 3.0) * Float64(Float64(-b) - sqrt(fma(-3.0, Float64(a * c), Float64(b * b)))))) end
code[a_, b_, c_] := N[(N[(a * N[(c * 3.0), $MachinePrecision] + 0.0), $MachinePrecision] / N[(N[(a * 3.0), $MachinePrecision] * N[((-b) - N[Sqrt[N[(-3.0 * N[(a * c), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(a, c \cdot 3, 0\right)}{\left(a \cdot 3\right) \cdot \left(\left(-b\right) - \sqrt{\mathsf{fma}\left(-3, a \cdot c, b \cdot b\right)}\right)}
\end{array}
Initial program 32.7%
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6432.7
lift--.f64N/A
sub-negN/A
+-commutativeN/A
lift-*.f64N/A
distribute-lft-neg-inN/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
associate-*l*N/A
lower-fma.f64N/A
lower-*.f64N/A
metadata-eval32.7
Applied rewrites32.7%
Applied rewrites99.2%
(FPCore (a b c) :precision binary64 (/ (fma a (/ (* (* c c) -0.375) (* b b)) (* c -0.5)) b))
double code(double a, double b, double c) {
return fma(a, (((c * c) * -0.375) / (b * b)), (c * -0.5)) / b;
}
function code(a, b, c) return Float64(fma(a, Float64(Float64(Float64(c * c) * -0.375) / Float64(b * b)), Float64(c * -0.5)) / b) end
code[a_, b_, c_] := N[(N[(a * N[(N[(N[(c * c), $MachinePrecision] * -0.375), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + N[(c * -0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(a, \frac{\left(c \cdot c\right) \cdot -0.375}{b \cdot b}, c \cdot -0.5\right)}{b}
\end{array}
Initial program 32.7%
Taylor expanded in b around inf
lower-/.f64N/A
Applied rewrites90.8%
(FPCore (a b c) :precision binary64 (/ (* c (fma a (* -0.375 (/ c (* b b))) -0.5)) b))
double code(double a, double b, double c) {
return (c * fma(a, (-0.375 * (c / (b * b))), -0.5)) / b;
}
function code(a, b, c) return Float64(Float64(c * fma(a, Float64(-0.375 * Float64(c / Float64(b * b))), -0.5)) / b) end
code[a_, b_, c_] := N[(N[(c * N[(a * N[(-0.375 * N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot \mathsf{fma}\left(a, -0.375 \cdot \frac{c}{b \cdot b}, -0.5\right)}{b}
\end{array}
Initial program 32.7%
Taylor expanded in b around inf
lower-/.f64N/A
Applied rewrites90.8%
Taylor expanded in c around 0
Applied rewrites90.7%
Final simplification90.7%
(FPCore (a b c) :precision binary64 (* c (/ (fma a (* -0.375 (/ c (* b b))) -0.5) b)))
double code(double a, double b, double c) {
return c * (fma(a, (-0.375 * (c / (b * b))), -0.5) / b);
}
function code(a, b, c) return Float64(c * Float64(fma(a, Float64(-0.375 * Float64(c / Float64(b * b))), -0.5) / b)) end
code[a_, b_, c_] := N[(c * N[(N[(a * N[(-0.375 * N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -0.5), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \frac{\mathsf{fma}\left(a, -0.375 \cdot \frac{c}{b \cdot b}, -0.5\right)}{b}
\end{array}
Initial program 32.7%
Taylor expanded in a around 0
Applied rewrites95.9%
Taylor expanded in c around 0
lower-*.f64N/A
unpow3N/A
unpow2N/A
associate-/r*N/A
associate-/l*N/A
associate-*r/N/A
metadata-evalN/A
div-subN/A
lower-/.f64N/A
Applied rewrites90.5%
Final simplification90.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 32.7%
Taylor expanded in b around inf
lower-*.f64N/A
lower-/.f6480.6
Applied rewrites80.6%
herbie shell --seed 2024223
(FPCore (a b c)
:name "Cubic critical, medium range"
:precision binary64
:pre (and (and (and (< 1.1102230246251565e-16 a) (< a 9007199254740992.0)) (and (< 1.1102230246251565e-16 b) (< b 9007199254740992.0))) (and (< 1.1102230246251565e-16 c) (< c 9007199254740992.0)))
(/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))