
(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 6 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
(let* ((t_0 (* b (* b b))) (t_1 (* c (* c c))))
(/
(fma
c
-0.5
(fma
a
(/ (fma -0.5625 (/ (* a t_1) (* b b)) (* (* c c) -0.375)) (* b b))
(/ (* -1.0546875 (* a (* (* c t_1) (* a a)))) (* t_0 t_0))))
b)))
double code(double a, double b, double c) {
double t_0 = b * (b * b);
double t_1 = c * (c * c);
return fma(c, -0.5, fma(a, (fma(-0.5625, ((a * t_1) / (b * b)), ((c * c) * -0.375)) / (b * b)), ((-1.0546875 * (a * ((c * t_1) * (a * a)))) / (t_0 * t_0)))) / b;
}
function code(a, b, c) t_0 = Float64(b * Float64(b * b)) t_1 = Float64(c * Float64(c * c)) return Float64(fma(c, -0.5, fma(a, Float64(fma(-0.5625, Float64(Float64(a * t_1) / Float64(b * b)), Float64(Float64(c * c) * -0.375)) / Float64(b * b)), Float64(Float64(-1.0546875 * Float64(a * Float64(Float64(c * t_1) * Float64(a * a)))) / Float64(t_0 * t_0)))) / b) end
code[a_, b_, c_] := Block[{t$95$0 = N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(c * N[(c * c), $MachinePrecision]), $MachinePrecision]}, N[(N[(c * -0.5 + N[(a * N[(N[(-0.5625 * N[(N[(a * t$95$1), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + N[(N[(c * c), $MachinePrecision] * -0.375), $MachinePrecision]), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + N[(N[(-1.0546875 * N[(a * N[(N[(c * t$95$1), $MachinePrecision] * N[(a * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(t$95$0 * t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := b \cdot \left(b \cdot b\right)\\
t_1 := c \cdot \left(c \cdot c\right)\\
\frac{\mathsf{fma}\left(c, -0.5, \mathsf{fma}\left(a, \frac{\mathsf{fma}\left(-0.5625, \frac{a \cdot t\_1}{b \cdot b}, \left(c \cdot c\right) \cdot -0.375\right)}{b \cdot b}, \frac{-1.0546875 \cdot \left(a \cdot \left(\left(c \cdot t\_1\right) \cdot \left(a \cdot a\right)\right)\right)}{t\_0 \cdot t\_0}\right)\right)}{b}
\end{array}
\end{array}
Initial program 32.9%
Taylor expanded in a around 0
Applied rewrites95.0%
Taylor expanded in b around inf
Applied rewrites95.0%
Applied rewrites95.0%
Taylor expanded in b around inf
lower-/.f64N/A
lower-fma.f64N/A
lower-/.f64N/A
lower-*.f64N/A
cube-multN/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6495.0
Applied rewrites95.0%
Final simplification95.0%
(FPCore (a b c) :precision binary64 (/ 1.0 (fma a (fma a (/ (* c 1.125) (* b (* b b))) (/ 1.5 b)) (* (/ b c) -2.0))))
double code(double a, double b, double c) {
return 1.0 / fma(a, fma(a, ((c * 1.125) / (b * (b * b))), (1.5 / b)), ((b / c) * -2.0));
}
function code(a, b, c) return Float64(1.0 / fma(a, fma(a, Float64(Float64(c * 1.125) / Float64(b * Float64(b * b))), Float64(1.5 / b)), Float64(Float64(b / c) * -2.0))) end
code[a_, b_, c_] := N[(1.0 / N[(a * N[(a * N[(N[(c * 1.125), $MachinePrecision] / N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(1.5 / b), $MachinePrecision]), $MachinePrecision] + N[(N[(b / c), $MachinePrecision] * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(a, \mathsf{fma}\left(a, \frac{c \cdot 1.125}{b \cdot \left(b \cdot b\right)}, \frac{1.5}{b}\right), \frac{b}{c} \cdot -2\right)}
\end{array}
Initial program 32.9%
Taylor expanded in b around inf
lower-/.f64N/A
Applied rewrites93.1%
Applied rewrites92.8%
Taylor expanded in a around 0
+-commutativeN/A
lower-fma.f64N/A
Applied rewrites93.2%
Final simplification93.2%
(FPCore (a b c) :precision binary64 (/ (/ 1.0 (fma a (/ 1.5 (* b b)) (/ -2.0 c))) b))
double code(double a, double b, double c) {
return (1.0 / fma(a, (1.5 / (b * b)), (-2.0 / c))) / b;
}
function code(a, b, c) return Float64(Float64(1.0 / fma(a, Float64(1.5 / Float64(b * b)), Float64(-2.0 / c))) / b) end
code[a_, b_, c_] := N[(N[(1.0 / N[(a * N[(1.5 / N[(b * b), $MachinePrecision]), $MachinePrecision] + N[(-2.0 / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{\mathsf{fma}\left(a, \frac{1.5}{b \cdot b}, \frac{-2}{c}\right)}}{b}
\end{array}
Initial program 32.9%
Taylor expanded in b around inf
lower-/.f64N/A
Applied rewrites93.1%
Applied rewrites92.8%
Taylor expanded in b around inf
lower-*.f64N/A
sub-negN/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
distribute-neg-fracN/A
metadata-evalN/A
lower-/.f6489.7
Applied rewrites89.7%
lift-*.f64N/A
lift-/.f64N/A
lift-/.f64N/A
lift-fma.f64N/A
*-commutativeN/A
associate-/r*N/A
lower-/.f64N/A
lower-/.f6489.9
lift-fma.f64N/A
lift-/.f64N/A
associate-*l/N/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6489.9
Applied rewrites89.9%
(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 32.9%
Taylor expanded in b around inf
lower-/.f64N/A
Applied rewrites93.1%
Applied rewrites92.8%
Taylor expanded in b around inf
lower-*.f64N/A
sub-negN/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
distribute-neg-fracN/A
metadata-evalN/A
lower-/.f6489.7
Applied rewrites89.7%
Taylor expanded in a around 0
lower-fma.f64N/A
lower-/.f64N/A
lower-*.f64N/A
lower-/.f6489.8
Applied rewrites89.8%
(FPCore (a b c) :precision binary64 (/ (* c (fma -0.375 (/ (* c a) (* b b)) -0.5)) b))
double code(double a, double b, double c) {
return (c * fma(-0.375, ((c * a) / (b * b)), -0.5)) / b;
}
function code(a, b, c) return Float64(Float64(c * fma(-0.375, Float64(Float64(c * a) / Float64(b * b)), -0.5)) / b) end
code[a_, b_, c_] := N[(N[(c * N[(-0.375 * N[(N[(c * a), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + -0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot \mathsf{fma}\left(-0.375, \frac{c \cdot a}{b \cdot b}, -0.5\right)}{b}
\end{array}
Initial program 32.9%
Taylor expanded in a around 0
Applied rewrites95.0%
Taylor expanded in b around inf
Applied rewrites95.0%
Taylor expanded in c around 0
lower-*.f64N/A
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6489.6
Applied rewrites89.6%
(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.9%
Taylor expanded in b around inf
lower-*.f64N/A
lower-/.f6480.3
Applied rewrites80.3%
herbie shell --seed 2024216
(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)))