
(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 7 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 (/ (/ (fma a (* -4.0 c) 0.0) (+ b (sqrt (fma a (* -4.0 c) (* b b))))) (* a 2.0)))
double code(double a, double b, double c) {
return (fma(a, (-4.0 * c), 0.0) / (b + sqrt(fma(a, (-4.0 * c), (b * b))))) / (a * 2.0);
}
function code(a, b, c) return Float64(Float64(fma(a, Float64(-4.0 * c), 0.0) / Float64(b + sqrt(fma(a, Float64(-4.0 * c), Float64(b * b))))) / Float64(a * 2.0)) end
code[a_, b_, c_] := N[(N[(N[(a * N[(-4.0 * c), $MachinePrecision] + 0.0), $MachinePrecision] / N[(b + N[Sqrt[N[(a * N[(-4.0 * c), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\mathsf{fma}\left(a, -4 \cdot c, 0\right)}{b + \sqrt{\mathsf{fma}\left(a, -4 \cdot c, b \cdot b\right)}}}{a \cdot 2}
\end{array}
Initial program 20.0%
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6420.0
lift--.f64N/A
sub-negN/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
lower-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
metadata-eval20.0
Applied rewrites20.0%
lift--.f64N/A
flip--N/A
div-invN/A
lower-*.f64N/A
Applied rewrites20.9%
lift-*.f64N/A
lift-/.f64N/A
un-div-invN/A
lower-/.f6420.9
lift--.f64N/A
lift-fma.f64N/A
associate--l+N/A
*-commutativeN/A
lift-*.f64N/A
associate-*l*N/A
+-inversesN/A
lower-fma.f64N/A
lower-*.f6499.4
lift-fma.f64N/A
*-commutativeN/A
lift-*.f64N/A
associate-*l*N/A
lower-fma.f64N/A
lower-*.f6499.4
Applied rewrites99.4%
Final simplification99.4%
(FPCore (a b c) :precision binary64 (/ (* a (* -4.0 c)) (* (+ b (sqrt (fma a (* -4.0 c) (* b b)))) (* a 2.0))))
double code(double a, double b, double c) {
return (a * (-4.0 * c)) / ((b + sqrt(fma(a, (-4.0 * c), (b * b)))) * (a * 2.0));
}
function code(a, b, c) return Float64(Float64(a * Float64(-4.0 * c)) / Float64(Float64(b + sqrt(fma(a, Float64(-4.0 * c), Float64(b * b)))) * Float64(a * 2.0))) end
code[a_, b_, c_] := N[(N[(a * N[(-4.0 * c), $MachinePrecision]), $MachinePrecision] / N[(N[(b + N[Sqrt[N[(a * N[(-4.0 * c), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[(a * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{a \cdot \left(-4 \cdot c\right)}{\left(b + \sqrt{\mathsf{fma}\left(a, -4 \cdot c, b \cdot b\right)}\right) \cdot \left(a \cdot 2\right)}
\end{array}
Initial program 18.6%
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6418.6
lift--.f64N/A
sub-negN/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
lower-fma.f64N/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
lower-*.f64N/A
metadata-eval18.6
Applied rewrites18.6%
lift--.f64N/A
flip--N/A
div-invN/A
lower-*.f64N/A
Applied rewrites19.1%
lift-*.f64N/A
lift-/.f64N/A
un-div-invN/A
lower-/.f6419.1
lift--.f64N/A
lift-fma.f64N/A
associate--l+N/A
*-commutativeN/A
lift-*.f64N/A
associate-*l*N/A
+-inversesN/A
lower-fma.f64N/A
lower-*.f6499.4
lift-fma.f64N/A
*-commutativeN/A
lift-*.f64N/A
associate-*l*N/A
lower-fma.f64N/A
lower-*.f6499.4
Applied rewrites99.4%
lift-/.f64N/A
lift-/.f64N/A
lift-fma.f64N/A
+-rgt-identityN/A
associate-/l/N/A
lower-/.f64N/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f6499.4
lift-*.f64N/A
*-commutativeN/A
lower-*.f6499.4
Applied rewrites99.4%
herbie shell --seed 2024228
(FPCore (a b c)
:name "Quadratic roots, 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) (* (* 4.0 a) c)))) (* 2.0 a)))