
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.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) - ((4.0d0 * a) * c)))) / (2.0d0 * a)
end function
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c) :precision binary64 (/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))
double code(double a, double b, double c) {
return (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.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) - ((4.0d0 * a) * c)))) / (2.0d0 * a)
end function
public static double code(double a, double b, double c) {
return (-b + Math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a);
}
def code(a, b, c): return (-b + math.sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a)
function code(a, b, c) return Float64(Float64(Float64(-b) + sqrt(Float64(Float64(b * b) - Float64(Float64(4.0 * a) * c)))) / Float64(2.0 * a)) end
function tmp = code(a, b, c) tmp = (-b + sqrt(((b * b) - ((4.0 * a) * c)))) / (2.0 * a); end
code[a_, b_, c_] := N[(N[((-b) + N[Sqrt[N[(N[(b * b), $MachinePrecision] - N[(N[(4.0 * a), $MachinePrecision] * c), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-b\right) + \sqrt{b \cdot b - \left(4 \cdot a\right) \cdot c}}{2 \cdot a}
\end{array}
(FPCore (a b c)
:precision binary64
(let* ((t_0 (* (* c a) c)) (t_1 (/ (* c c) (* b b))))
(/
(fma
(* (* -2.0 a) a)
(* (/ c (* b b)) t_1)
(fma a (- (* (/ (* t_0 t_0) (pow b 6.0)) -5.0) t_1) (- c)))
b)))
double code(double a, double b, double c) {
double t_0 = (c * a) * c;
double t_1 = (c * c) / (b * b);
return fma(((-2.0 * a) * a), ((c / (b * b)) * t_1), fma(a, ((((t_0 * t_0) / pow(b, 6.0)) * -5.0) - t_1), -c)) / b;
}
function code(a, b, c) t_0 = Float64(Float64(c * a) * c) t_1 = Float64(Float64(c * c) / Float64(b * b)) return Float64(fma(Float64(Float64(-2.0 * a) * a), Float64(Float64(c / Float64(b * b)) * t_1), fma(a, Float64(Float64(Float64(Float64(t_0 * t_0) / (b ^ 6.0)) * -5.0) - t_1), Float64(-c))) / b) end
code[a_, b_, c_] := Block[{t$95$0 = N[(N[(c * a), $MachinePrecision] * c), $MachinePrecision]}, Block[{t$95$1 = N[(N[(c * c), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(N[(-2.0 * a), $MachinePrecision] * a), $MachinePrecision] * N[(N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision] * t$95$1), $MachinePrecision] + N[(a * N[(N[(N[(N[(t$95$0 * t$95$0), $MachinePrecision] / N[Power[b, 6.0], $MachinePrecision]), $MachinePrecision] * -5.0), $MachinePrecision] - t$95$1), $MachinePrecision] + (-c)), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(c \cdot a\right) \cdot c\\
t_1 := \frac{c \cdot c}{b \cdot b}\\
\frac{\mathsf{fma}\left(\left(-2 \cdot a\right) \cdot a, \frac{c}{b \cdot b} \cdot t\_1, \mathsf{fma}\left(a, \frac{t\_0 \cdot t\_0}{{b}^{6}} \cdot -5 - t\_1, -c\right)\right)}{b}
\end{array}
\end{array}
Initial program 32.9%
Taylor expanded in b around inf
Applied rewrites95.8%
Applied rewrites95.8%
Taylor expanded in a around 0
Applied rewrites95.9%
Applied rewrites95.9%
Final simplification95.9%
(FPCore (a b c) :precision binary64 (/ 1.0 (fma -1.0 (/ b c) (* (fma -2.0 (* (* -0.5 (/ c (pow b 3.0))) a) (/ 1.0 b)) a))))
double code(double a, double b, double c) {
return 1.0 / fma(-1.0, (b / c), (fma(-2.0, ((-0.5 * (c / pow(b, 3.0))) * a), (1.0 / b)) * a));
}
function code(a, b, c) return Float64(1.0 / fma(-1.0, Float64(b / c), Float64(fma(-2.0, Float64(Float64(-0.5 * Float64(c / (b ^ 3.0))) * a), Float64(1.0 / b)) * a))) end
code[a_, b_, c_] := N[(1.0 / N[(-1.0 * N[(b / c), $MachinePrecision] + N[(N[(-2.0 * N[(N[(-0.5 * N[(c / N[Power[b, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * a), $MachinePrecision] + N[(1.0 / b), $MachinePrecision]), $MachinePrecision] * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(-1, \frac{b}{c}, \mathsf{fma}\left(-2, \left(-0.5 \cdot \frac{c}{{b}^{3}}\right) \cdot a, \frac{1}{b}\right) \cdot a\right)}
\end{array}
Initial program 32.9%
Taylor expanded in c around inf
*-commutativeN/A
lower-*.f64N/A
cancel-sign-sub-invN/A
metadata-evalN/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6432.8
Applied rewrites32.8%
lift-/.f64N/A
clear-numN/A
lower-/.f64N/A
lower-/.f6432.8
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6432.8
Applied rewrites32.8%
Taylor expanded in a around 0
lower-fma.f64N/A
lower-/.f64N/A
lower-*.f64N/A
lower-fma.f64N/A
lower-*.f64N/A
distribute-rgt-outN/A
metadata-evalN/A
lower-*.f64N/A
lower-/.f64N/A
lower-pow.f64N/A
lower-/.f6494.2
Applied rewrites94.2%
Final simplification94.2%
(FPCore (a b c) :precision binary64 (/ (fma (* (* -2.0 a) a) (* (/ c (* b b)) (/ (* c c) (* b b))) (- (- c) (/ (* (* c c) a) (* b b)))) b))
double code(double a, double b, double c) {
return fma(((-2.0 * a) * a), ((c / (b * b)) * ((c * c) / (b * b))), (-c - (((c * c) * a) / (b * b)))) / b;
}
function code(a, b, c) return Float64(fma(Float64(Float64(-2.0 * a) * a), Float64(Float64(c / Float64(b * b)) * Float64(Float64(c * c) / Float64(b * b))), Float64(Float64(-c) - Float64(Float64(Float64(c * c) * a) / Float64(b * b)))) / b) end
code[a_, b_, c_] := N[(N[(N[(N[(-2.0 * a), $MachinePrecision] * a), $MachinePrecision] * N[(N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision] * N[(N[(c * c), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[((-c) - N[(N[(N[(c * c), $MachinePrecision] * a), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(\left(-2 \cdot a\right) \cdot a, \frac{c}{b \cdot b} \cdot \frac{c \cdot c}{b \cdot b}, \left(-c\right) - \frac{\left(c \cdot c\right) \cdot a}{b \cdot b}\right)}{b}
\end{array}
Initial program 32.9%
Taylor expanded in b around inf
Applied rewrites95.8%
Applied rewrites95.8%
Taylor expanded in a around 0
Applied rewrites94.2%
Final simplification94.2%
(FPCore (a b c) :precision binary64 (/ 1.0 (fma -1.0 (/ b c) (/ a b))))
double code(double a, double b, double c) {
return 1.0 / fma(-1.0, (b / c), (a / b));
}
function code(a, b, c) return Float64(1.0 / fma(-1.0, Float64(b / c), Float64(a / b))) end
code[a_, b_, c_] := N[(1.0 / N[(-1.0 * N[(b / c), $MachinePrecision] + N[(a / b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\mathsf{fma}\left(-1, \frac{b}{c}, \frac{a}{b}\right)}
\end{array}
Initial program 32.9%
Taylor expanded in c around inf
*-commutativeN/A
lower-*.f64N/A
cancel-sign-sub-invN/A
metadata-evalN/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
associate-/l*N/A
lower-*.f64N/A
lower-/.f6432.8
Applied rewrites32.8%
lift-/.f64N/A
clear-numN/A
lower-/.f64N/A
lower-/.f6432.8
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6432.8
Applied rewrites32.8%
Taylor expanded in a around 0
lower-fma.f64N/A
lower-/.f64N/A
lower-/.f6490.9
Applied rewrites90.9%
(FPCore (a b c) :precision binary64 (/ (- c) b))
double code(double a, double b, double c) {
return -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 = -c / b
end function
public static double code(double a, double b, double c) {
return -c / b;
}
def code(a, b, c): return -c / b
function code(a, b, c) return Float64(Float64(-c) / b) end
function tmp = code(a, b, c) tmp = -c / b; end
code[a_, b_, c_] := N[((-c) / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{-c}{b}
\end{array}
Initial program 32.9%
Taylor expanded in a around 0
associate-*r/N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6480.4
Applied rewrites80.4%
herbie shell --seed 2024283
(FPCore (a b c)
:name "Quadratic roots, 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) (* (* 4.0 a) c)))) (* 2.0 a)))