
(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 6 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 (* (* c 2.0) (/ -1.0 (+ b (sqrt (fma b b (* -4.0 (* c a))))))))
double code(double a, double b, double c) {
return (c * 2.0) * (-1.0 / (b + sqrt(fma(b, b, (-4.0 * (c * a))))));
}
function code(a, b, c) return Float64(Float64(c * 2.0) * Float64(-1.0 / Float64(b + sqrt(fma(b, b, Float64(-4.0 * Float64(c * a))))))) end
code[a_, b_, c_] := N[(N[(c * 2.0), $MachinePrecision] * N[(-1.0 / N[(b + N[Sqrt[N[(b * b + N[(-4.0 * N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(c \cdot 2\right) \cdot \frac{-1}{b + \sqrt{\mathsf{fma}\left(b, b, -4 \cdot \left(c \cdot a\right)\right)}}
\end{array}
Initial program 55.3%
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
sub-negN/A
lift-*.f64N/A
lower-fma.f64N/A
lift-*.f64N/A
distribute-lft-neg-inN/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
associate-*l*N/A
lower-*.f64N/A
lower-*.f64N/A
metadata-eval55.4
Applied egg-rr55.4%
Applied egg-rr56.6%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f6499.4
Simplified99.4%
Final simplification99.4%
(FPCore (a b c) :precision binary64 (if (<= b 3.4) (* (/ -0.5 a) (- b (sqrt (fma a (* c -4.0) (* b b))))) (* (* c 2.0) (/ 1.0 (* 2.0 (- (/ (* c a) b) b))))))
double code(double a, double b, double c) {
double tmp;
if (b <= 3.4) {
tmp = (-0.5 / a) * (b - sqrt(fma(a, (c * -4.0), (b * b))));
} else {
tmp = (c * 2.0) * (1.0 / (2.0 * (((c * a) / b) - b)));
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 3.4) tmp = Float64(Float64(-0.5 / a) * Float64(b - sqrt(fma(a, Float64(c * -4.0), Float64(b * b))))); else tmp = Float64(Float64(c * 2.0) * Float64(1.0 / Float64(2.0 * Float64(Float64(Float64(c * a) / b) - b)))); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 3.4], N[(N[(-0.5 / a), $MachinePrecision] * N[(b - N[Sqrt[N[(a * N[(c * -4.0), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(c * 2.0), $MachinePrecision] * N[(1.0 / N[(2.0 * N[(N[(N[(c * a), $MachinePrecision] / b), $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 3.4:\\
\;\;\;\;\frac{-0.5}{a} \cdot \left(b - \sqrt{\mathsf{fma}\left(a, c \cdot -4, b \cdot b\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;\left(c \cdot 2\right) \cdot \frac{1}{2 \cdot \left(\frac{c \cdot a}{b} - b\right)}\\
\end{array}
\end{array}
if b < 3.39999999999999991Initial program 85.0%
Applied egg-rr85.0%
if 3.39999999999999991 < b Initial program 46.9%
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
sub-negN/A
lift-*.f64N/A
lower-fma.f64N/A
lift-*.f64N/A
distribute-lft-neg-inN/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
associate-*l*N/A
lower-*.f64N/A
lower-*.f64N/A
metadata-eval47.1
Applied egg-rr47.1%
Applied egg-rr48.4%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f6499.4
Simplified99.4%
Taylor expanded in c around 0
distribute-lft-out--N/A
lower-*.f64N/A
lower--.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6488.8
Simplified88.8%
Final simplification88.0%
(FPCore (a b c) :precision binary64 (* (* c 2.0) (/ 1.0 (* 2.0 (- (/ (* c a) b) b)))))
double code(double a, double b, double c) {
return (c * 2.0) * (1.0 / (2.0 * (((c * a) / b) - 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 * 2.0d0) * (1.0d0 / (2.0d0 * (((c * a) / b) - b)))
end function
public static double code(double a, double b, double c) {
return (c * 2.0) * (1.0 / (2.0 * (((c * a) / b) - b)));
}
def code(a, b, c): return (c * 2.0) * (1.0 / (2.0 * (((c * a) / b) - b)))
function code(a, b, c) return Float64(Float64(c * 2.0) * Float64(1.0 / Float64(2.0 * Float64(Float64(Float64(c * a) / b) - b)))) end
function tmp = code(a, b, c) tmp = (c * 2.0) * (1.0 / (2.0 * (((c * a) / b) - b))); end
code[a_, b_, c_] := N[(N[(c * 2.0), $MachinePrecision] * N[(1.0 / N[(2.0 * N[(N[(N[(c * a), $MachinePrecision] / b), $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(c \cdot 2\right) \cdot \frac{1}{2 \cdot \left(\frac{c \cdot a}{b} - b\right)}
\end{array}
Initial program 55.3%
lift-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
sub-negN/A
lift-*.f64N/A
lower-fma.f64N/A
lift-*.f64N/A
distribute-lft-neg-inN/A
lift-*.f64N/A
*-commutativeN/A
distribute-rgt-neg-inN/A
associate-*l*N/A
lower-*.f64N/A
lower-*.f64N/A
metadata-eval55.4
Applied egg-rr55.4%
Applied egg-rr56.6%
Taylor expanded in a around 0
*-commutativeN/A
lower-*.f6499.4
Simplified99.4%
Taylor expanded in c around 0
distribute-lft-out--N/A
lower-*.f64N/A
lower--.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6481.7
Simplified81.7%
(FPCore (a b c) :precision binary64 (- (fma a (/ (* c c) (* b (* b b))) (/ c b))))
double code(double a, double b, double c) {
return -fma(a, ((c * c) / (b * (b * b))), (c / b));
}
function code(a, b, c) return Float64(-fma(a, Float64(Float64(c * c) / Float64(b * Float64(b * b))), Float64(c / b))) end
code[a_, b_, c_] := (-N[(a * N[(N[(c * c), $MachinePrecision] / N[(b * N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(c / b), $MachinePrecision]), $MachinePrecision])
\begin{array}{l}
\\
-\mathsf{fma}\left(a, \frac{c \cdot c}{b \cdot \left(b \cdot b\right)}, \frac{c}{b}\right)
\end{array}
Initial program 55.3%
Taylor expanded in a around 0
Simplified90.4%
Taylor expanded in a around 0
sub-negN/A
mul-1-negN/A
distribute-neg-outN/A
lower-neg.f64N/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
cube-multN/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f64N/A
lower-/.f6481.1
Simplified81.1%
(FPCore (a b c) :precision binary64 (/ (fma (* c c) (/ a (* b b)) c) (- b)))
double code(double a, double b, double c) {
return fma((c * c), (a / (b * b)), c) / -b;
}
function code(a, b, c) return Float64(fma(Float64(c * c), Float64(a / Float64(b * b)), c) / Float64(-b)) end
code[a_, b_, c_] := N[(N[(N[(c * c), $MachinePrecision] * N[(a / N[(b * b), $MachinePrecision]), $MachinePrecision] + c), $MachinePrecision] / (-b)), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(c \cdot c, \frac{a}{b \cdot b}, c\right)}{-b}
\end{array}
Initial program 55.3%
Taylor expanded in b around inf
distribute-lft-outN/A
associate-/l*N/A
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f6481.1
Simplified81.1%
Final simplification81.1%
(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(c / Float64(-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 55.3%
Taylor expanded in b around inf
mul-1-negN/A
distribute-neg-frac2N/A
lower-/.f64N/A
lower-neg.f6464.7
Simplified64.7%
herbie shell --seed 2024207
(FPCore (a b c)
:name "Quadratic roots, narrow range"
:precision binary64
:pre (and (and (and (< 1.0536712127723509e-8 a) (< a 94906265.62425156)) (and (< 1.0536712127723509e-8 b) (< b 94906265.62425156))) (and (< 1.0536712127723509e-8 c) (< c 94906265.62425156)))
(/ (+ (- b) (sqrt (- (* b b) (* (* 4.0 a) c)))) (* 2.0 a)))