
(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 10 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))))
(fma
(fma
c
(* c (/ -0.375 t_0))
(*
a
(fma
c
(* (* c c) (/ -0.5625 (* (* b b) t_0)))
(*
(/ (* (* c (* c (* c c))) (* a 6.328125)) (* b (* (* b b) (* b t_0))))
-0.16666666666666666))))
a
(/ (* c -0.5) b))))
double code(double a, double b, double c) {
double t_0 = b * (b * b);
return fma(fma(c, (c * (-0.375 / t_0)), (a * fma(c, ((c * c) * (-0.5625 / ((b * b) * t_0))), ((((c * (c * (c * c))) * (a * 6.328125)) / (b * ((b * b) * (b * t_0)))) * -0.16666666666666666)))), a, ((c * -0.5) / b));
}
function code(a, b, c) t_0 = Float64(b * Float64(b * b)) return fma(fma(c, Float64(c * Float64(-0.375 / t_0)), Float64(a * fma(c, Float64(Float64(c * c) * Float64(-0.5625 / Float64(Float64(b * b) * t_0))), Float64(Float64(Float64(Float64(c * Float64(c * Float64(c * c))) * Float64(a * 6.328125)) / Float64(b * Float64(Float64(b * b) * Float64(b * t_0)))) * -0.16666666666666666)))), a, Float64(Float64(c * -0.5) / b)) end
code[a_, b_, c_] := Block[{t$95$0 = N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]}, N[(N[(c * N[(c * N[(-0.375 / t$95$0), $MachinePrecision]), $MachinePrecision] + N[(a * N[(c * N[(N[(c * c), $MachinePrecision] * N[(-0.5625 / N[(N[(b * b), $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(N[(N[(c * N[(c * N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(a * 6.328125), $MachinePrecision]), $MachinePrecision] / N[(b * N[(N[(b * b), $MachinePrecision] * N[(b * t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * -0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * a + N[(N[(c * -0.5), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := b \cdot \left(b \cdot b\right)\\
\mathsf{fma}\left(\mathsf{fma}\left(c, c \cdot \frac{-0.375}{t\_0}, a \cdot \mathsf{fma}\left(c, \left(c \cdot c\right) \cdot \frac{-0.5625}{\left(b \cdot b\right) \cdot t\_0}, \frac{\left(c \cdot \left(c \cdot \left(c \cdot c\right)\right)\right) \cdot \left(a \cdot 6.328125\right)}{b \cdot \left(\left(b \cdot b\right) \cdot \left(b \cdot t\_0\right)\right)} \cdot -0.16666666666666666\right)\right), a, \frac{c \cdot -0.5}{b}\right)
\end{array}
\end{array}
Initial program 17.6%
Taylor expanded in a around 0
Simplified97.8%
Applied egg-rr97.8%
Final simplification97.8%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (* c (* c c))))
(/
(fma
(* a (* a t_0))
(/ -0.5625 (* (* b b) (* b b)))
(fma
c
-0.5
(fma
a
(/ (* c (* c -0.375)) (* b b))
(/
(* (* c t_0) (* (* a (* a a)) -1.0546875))
(* (* b b) (* b (* b (* b b))))))))
b)))
double code(double a, double b, double c) {
double t_0 = c * (c * c);
return fma((a * (a * t_0)), (-0.5625 / ((b * b) * (b * b))), fma(c, -0.5, fma(a, ((c * (c * -0.375)) / (b * b)), (((c * t_0) * ((a * (a * a)) * -1.0546875)) / ((b * b) * (b * (b * (b * b)))))))) / b;
}
function code(a, b, c) t_0 = Float64(c * Float64(c * c)) return Float64(fma(Float64(a * Float64(a * t_0)), Float64(-0.5625 / Float64(Float64(b * b) * Float64(b * b))), fma(c, -0.5, fma(a, Float64(Float64(c * Float64(c * -0.375)) / Float64(b * b)), Float64(Float64(Float64(c * t_0) * Float64(Float64(a * Float64(a * a)) * -1.0546875)) / Float64(Float64(b * b) * Float64(b * Float64(b * Float64(b * b)))))))) / b) end
code[a_, b_, c_] := Block[{t$95$0 = N[(c * N[(c * c), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(a * N[(a * t$95$0), $MachinePrecision]), $MachinePrecision] * N[(-0.5625 / N[(N[(b * b), $MachinePrecision] * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(c * -0.5 + N[(a * N[(N[(c * N[(c * -0.375), $MachinePrecision]), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + N[(N[(N[(c * t$95$0), $MachinePrecision] * N[(N[(a * N[(a * a), $MachinePrecision]), $MachinePrecision] * -1.0546875), $MachinePrecision]), $MachinePrecision] / N[(N[(b * b), $MachinePrecision] * N[(b * N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := c \cdot \left(c \cdot c\right)\\
\frac{\mathsf{fma}\left(a \cdot \left(a \cdot t\_0\right), \frac{-0.5625}{\left(b \cdot b\right) \cdot \left(b \cdot b\right)}, \mathsf{fma}\left(c, -0.5, \mathsf{fma}\left(a, \frac{c \cdot \left(c \cdot -0.375\right)}{b \cdot b}, \frac{\left(c \cdot t\_0\right) \cdot \left(\left(a \cdot \left(a \cdot a\right)\right) \cdot -1.0546875\right)}{\left(b \cdot b\right) \cdot \left(b \cdot \left(b \cdot \left(b \cdot b\right)\right)\right)}\right)\right)\right)}{b}
\end{array}
\end{array}
Initial program 17.6%
Taylor expanded in a around 0
Simplified97.8%
Taylor expanded in b around inf
Simplified97.8%
associate-+r+N/A
+-commutativeN/A
accelerator-lowering-fma.f64N/A
+-commutativeN/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f64N/A
associate-*r*N/A
*-commutativeN/A
*-lowering-*.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
*-lowering-*.f64N/A
associate-*r/N/A
/-lowering-/.f64N/A
Applied egg-rr97.8%
(FPCore (a b c) :precision binary64 (fma a (/ (fma c (* c -0.375) (/ (* a (* -0.5625 (* c (* c c)))) (* b b))) (* b (* b b))) (* -0.5 (/ c b))))
double code(double a, double b, double c) {
return fma(a, (fma(c, (c * -0.375), ((a * (-0.5625 * (c * (c * c)))) / (b * b))) / (b * (b * b))), (-0.5 * (c / b)));
}
function code(a, b, c) return fma(a, Float64(fma(c, Float64(c * -0.375), Float64(Float64(a * Float64(-0.5625 * Float64(c * Float64(c * c)))) / Float64(b * b))) / Float64(b * Float64(b * b))), Float64(-0.5 * Float64(c / b))) end
code[a_, b_, c_] := N[(a * N[(N[(c * N[(c * -0.375), $MachinePrecision] + N[(N[(a * N[(-0.5625 * N[(c * N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-0.5 * N[(c / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(a, \frac{\mathsf{fma}\left(c, c \cdot -0.375, \frac{a \cdot \left(-0.5625 \cdot \left(c \cdot \left(c \cdot c\right)\right)\right)}{b \cdot b}\right)}{b \cdot \left(b \cdot b\right)}, -0.5 \cdot \frac{c}{b}\right)
\end{array}
Initial program 17.6%
Taylor expanded in a around 0
Simplified97.8%
Taylor expanded in b around inf
/-lowering-/.f64N/A
Simplified96.9%
(FPCore (a b c) :precision binary64 (/ 1.0 (fma a (fma (* a 3.0) (* (/ c (* b (* b b))) 0.375) (/ 1.5 b)) (/ (* b -2.0) c))))
double code(double a, double b, double c) {
return 1.0 / fma(a, fma((a * 3.0), ((c / (b * (b * b))) * 0.375), (1.5 / b)), ((b * -2.0) / c));
}
function code(a, b, c) return Float64(1.0 / fma(a, fma(Float64(a * 3.0), Float64(Float64(c / Float64(b * Float64(b * b))) * 0.375), Float64(1.5 / b)), Float64(Float64(b * -2.0) / c))) end
code[a_, b_, c_] := N[(1.0 / N[(a * N[(N[(a * 3.0), $MachinePrecision] * N[(N[(c / N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * 0.375), $MachinePrecision] + N[(1.5 / b), $MachinePrecision]), $MachinePrecision] + N[(N[(b * -2.0), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(a, \mathsf{fma}\left(a \cdot 3, \frac{c}{b \cdot \left(b \cdot b\right)} \cdot 0.375, \frac{1.5}{b}\right), \frac{b \cdot -2}{c}\right)}
\end{array}
Initial program 17.6%
Applied egg-rr17.6%
clear-numN/A
/-lowering-/.f64N/A
un-div-invN/A
associate-/r/N/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
--lowering--.f64N/A
sqrt-lowering-sqrt.f64N/A
accelerator-lowering-fma.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
*-lowering-*.f6417.6
Applied egg-rr17.6%
Taylor expanded in a around 0
+-commutativeN/A
accelerator-lowering-fma.f64N/A
Simplified96.8%
Final simplification96.8%
(FPCore (a b c) :precision binary64 (* c (fma c (/ (fma -0.5625 (/ (* c (* a a)) (* b b)) (* -0.375 a)) (* b (* b b))) (/ -0.5 b))))
double code(double a, double b, double c) {
return c * fma(c, (fma(-0.5625, ((c * (a * a)) / (b * b)), (-0.375 * a)) / (b * (b * b))), (-0.5 / b));
}
function code(a, b, c) return Float64(c * fma(c, Float64(fma(-0.5625, Float64(Float64(c * Float64(a * a)) / Float64(b * b)), Float64(-0.375 * a)) / Float64(b * Float64(b * b))), Float64(-0.5 / b))) end
code[a_, b_, c_] := N[(c * N[(c * N[(N[(-0.5625 * N[(N[(c * N[(a * a), $MachinePrecision]), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + N[(-0.375 * a), $MachinePrecision]), $MachinePrecision] / N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(-0.5 / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \mathsf{fma}\left(c, \frac{\mathsf{fma}\left(-0.5625, \frac{c \cdot \left(a \cdot a\right)}{b \cdot b}, -0.375 \cdot a\right)}{b \cdot \left(b \cdot b\right)}, \frac{-0.5}{b}\right)
\end{array}
Initial program 17.6%
Taylor expanded in c around 0
Simplified96.6%
Taylor expanded in b around inf
/-lowering-/.f64N/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
cube-multN/A
unpow2N/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6496.6
Simplified96.6%
Final simplification96.6%
(FPCore (a b c) :precision binary64 (fma -0.5 (/ c b) (/ (* -0.375 (* a (* c c))) (* b (* b b)))))
double code(double a, double b, double c) {
return fma(-0.5, (c / b), ((-0.375 * (a * (c * c))) / (b * (b * b))));
}
function code(a, b, c) return fma(-0.5, Float64(c / b), Float64(Float64(-0.375 * Float64(a * Float64(c * c))) / Float64(b * Float64(b * b)))) end
code[a_, b_, c_] := 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}
\\
\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}
Initial program 17.6%
Taylor expanded in a around 0
Simplified97.8%
Taylor expanded in b around inf
Simplified97.8%
Taylor expanded in a around 0
accelerator-lowering-fma.f64N/A
/-lowering-/.f64N/A
associate-*r/N/A
/-lowering-/.f64N/A
*-lowering-*.f64N/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f64N/A
cube-multN/A
unpow2N/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6495.4
Simplified95.4%
(FPCore (a b c) :precision binary64 (/ (fma a (/ (* -0.375 (* c c)) (* b b)) (* c -0.5)) b))
double code(double a, double b, double c) {
return fma(a, ((-0.375 * (c * c)) / (b * b)), (c * -0.5)) / b;
}
function code(a, b, c) return Float64(fma(a, Float64(Float64(-0.375 * Float64(c * c)) / Float64(b * b)), Float64(c * -0.5)) / b) end
code[a_, b_, c_] := N[(N[(a * N[(N[(-0.375 * N[(c * c), $MachinePrecision]), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + N[(c * -0.5), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(a, \frac{-0.375 \cdot \left(c \cdot c\right)}{b \cdot b}, c \cdot -0.5\right)}{b}
\end{array}
Initial program 17.6%
Taylor expanded in b around inf
/-lowering-/.f64N/A
Simplified95.4%
Final simplification95.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 17.6%
Taylor expanded in b around inf
/-lowering-/.f64N/A
Simplified95.4%
Taylor expanded in c around 0
*-lowering-*.f64N/A
sub-negN/A
metadata-evalN/A
accelerator-lowering-fma.f64N/A
associate-/l*N/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
unpow2N/A
*-lowering-*.f6495.4
Simplified95.4%
(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(c * Float64(fma(-0.375, Float64(Float64(c * a) / Float64(b * b)), -0.5) / b)) end
code[a_, b_, c_] := N[(c * N[(N[(-0.375 * N[(N[(c * a), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + -0.5), $MachinePrecision] / b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \frac{\mathsf{fma}\left(-0.375, \frac{c \cdot a}{b \cdot b}, -0.5\right)}{b}
\end{array}
Initial program 17.6%
Taylor expanded in c around 0
Simplified96.6%
Taylor expanded in b around inf
/-lowering-/.f64N/A
sub-negN/A
metadata-evalN/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f64N/A
*-commutativeN/A
*-lowering-*.f64N/A
unpow2N/A
*-lowering-*.f6495.1
Simplified95.1%
(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 17.6%
Taylor expanded in b around inf
*-lowering-*.f64N/A
/-lowering-/.f6490.3
Simplified90.3%
herbie shell --seed 2024205
(FPCore (a b c)
:name "Cubic critical, wide range"
:precision binary64
:pre (and (and (and (< 4.930380657631324e-32 a) (< a 2.028240960365167e+31)) (and (< 4.930380657631324e-32 b) (< b 2.028240960365167e+31))) (and (< 4.930380657631324e-32 c) (< c 2.028240960365167e+31)))
(/ (+ (- b) (sqrt (- (* b b) (* (* 3.0 a) c)))) (* 3.0 a)))