
(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 8 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)
(*
(* c c)
(fma
(/ (fma (* (pow a 3.0) 5.0) c (* (* b b) (* (* a a) 2.0))) (pow b 6.0))
c
(/ a (* b b)))))
b))
double code(double a, double b, double c) {
return (-c - ((c * c) * fma((fma((pow(a, 3.0) * 5.0), c, ((b * b) * ((a * a) * 2.0))) / pow(b, 6.0)), c, (a / (b * b))))) / b;
}
function code(a, b, c) return Float64(Float64(Float64(-c) - Float64(Float64(c * c) * fma(Float64(fma(Float64((a ^ 3.0) * 5.0), c, Float64(Float64(b * b) * Float64(Float64(a * a) * 2.0))) / (b ^ 6.0)), c, Float64(a / Float64(b * b))))) / b) end
code[a_, b_, c_] := N[(N[((-c) - N[(N[(c * c), $MachinePrecision] * N[(N[(N[(N[(N[Power[a, 3.0], $MachinePrecision] * 5.0), $MachinePrecision] * c + N[(N[(b * b), $MachinePrecision] * N[(N[(a * a), $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Power[b, 6.0], $MachinePrecision]), $MachinePrecision] * c + N[(a / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-c\right) - \left(c \cdot c\right) \cdot \mathsf{fma}\left(\frac{\mathsf{fma}\left({a}^{3} \cdot 5, c, \left(b \cdot b\right) \cdot \left(\left(a \cdot a\right) \cdot 2\right)\right)}{{b}^{6}}, c, \frac{a}{b \cdot b}\right)}{b}
\end{array}
Initial program 53.5%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites93.8%
Taylor expanded in b around -inf
Applied rewrites93.9%
Taylor expanded in c around 0
Applied rewrites93.9%
Taylor expanded in b around 0
Applied rewrites93.9%
Final simplification93.9%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (fma (* -4.0 c) a (* b b))))
(if (<= b 7.4)
(/ (- t_0 (* b b)) (* (+ (sqrt t_0) b) (* 2.0 a)))
(/
(fma
c
(* (fma (/ (/ (* (* a a) c) (* b b)) (* b b)) 2.0 (/ a (* b b))) c)
c)
(- b)))))
double code(double a, double b, double c) {
double t_0 = fma((-4.0 * c), a, (b * b));
double tmp;
if (b <= 7.4) {
tmp = (t_0 - (b * b)) / ((sqrt(t_0) + b) * (2.0 * a));
} else {
tmp = fma(c, (fma(((((a * a) * c) / (b * b)) / (b * b)), 2.0, (a / (b * b))) * c), c) / -b;
}
return tmp;
}
function code(a, b, c) t_0 = fma(Float64(-4.0 * c), a, Float64(b * b)) tmp = 0.0 if (b <= 7.4) tmp = Float64(Float64(t_0 - Float64(b * b)) / Float64(Float64(sqrt(t_0) + b) * Float64(2.0 * a))); else tmp = Float64(fma(c, Float64(fma(Float64(Float64(Float64(Float64(a * a) * c) / Float64(b * b)) / Float64(b * b)), 2.0, Float64(a / Float64(b * b))) * c), c) / Float64(-b)); end return tmp end
code[a_, b_, c_] := Block[{t$95$0 = N[(N[(-4.0 * c), $MachinePrecision] * a + N[(b * b), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[b, 7.4], N[(N[(t$95$0 - N[(b * b), $MachinePrecision]), $MachinePrecision] / N[(N[(N[Sqrt[t$95$0], $MachinePrecision] + b), $MachinePrecision] * N[(2.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(c * N[(N[(N[(N[(N[(N[(a * a), $MachinePrecision] * c), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] * 2.0 + N[(a / N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * c), $MachinePrecision] + c), $MachinePrecision] / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(-4 \cdot c, a, b \cdot b\right)\\
\mathbf{if}\;b \leq 7.4:\\
\;\;\;\;\frac{t\_0 - b \cdot b}{\left(\sqrt{t\_0} + b\right) \cdot \left(2 \cdot a\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(c, \mathsf{fma}\left(\frac{\frac{\left(a \cdot a\right) \cdot c}{b \cdot b}}{b \cdot b}, 2, \frac{a}{b \cdot b}\right) \cdot c, c\right)}{-b}\\
\end{array}
\end{array}
if b < 7.4000000000000004Initial program 84.5%
Applied rewrites84.5%
Applied rewrites85.9%
if 7.4000000000000004 < b Initial program 48.4%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites96.2%
Taylor expanded in b around -inf
Applied rewrites96.3%
Taylor expanded in c around 0
Applied rewrites93.7%
Applied rewrites93.7%
Final simplification92.6%
(FPCore (a b c)
:precision binary64
(let* ((t_0 (fma (* -4.0 c) a (* b b))))
(if (<= b 10.0)
(/ (- t_0 (* b b)) (* (+ (sqrt t_0) b) (* 2.0 a)))
(/ (fma a (/ (* c c) (* b b)) c) (- b)))))
double code(double a, double b, double c) {
double t_0 = fma((-4.0 * c), a, (b * b));
double tmp;
if (b <= 10.0) {
tmp = (t_0 - (b * b)) / ((sqrt(t_0) + b) * (2.0 * a));
} else {
tmp = fma(a, ((c * c) / (b * b)), c) / -b;
}
return tmp;
}
function code(a, b, c) t_0 = fma(Float64(-4.0 * c), a, Float64(b * b)) tmp = 0.0 if (b <= 10.0) tmp = Float64(Float64(t_0 - Float64(b * b)) / Float64(Float64(sqrt(t_0) + b) * Float64(2.0 * a))); else tmp = Float64(fma(a, Float64(Float64(c * c) / Float64(b * b)), c) / Float64(-b)); end return tmp end
code[a_, b_, c_] := Block[{t$95$0 = N[(N[(-4.0 * c), $MachinePrecision] * a + N[(b * b), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[b, 10.0], N[(N[(t$95$0 - N[(b * b), $MachinePrecision]), $MachinePrecision] / N[(N[(N[Sqrt[t$95$0], $MachinePrecision] + b), $MachinePrecision] * N[(2.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(a * N[(N[(c * c), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + c), $MachinePrecision] / (-b)), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(-4 \cdot c, a, b \cdot b\right)\\
\mathbf{if}\;b \leq 10:\\
\;\;\;\;\frac{t\_0 - b \cdot b}{\left(\sqrt{t\_0} + b\right) \cdot \left(2 \cdot a\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c \cdot c}{b \cdot b}, c\right)}{-b}\\
\end{array}
\end{array}
if b < 10Initial program 84.0%
Applied rewrites84.0%
Applied rewrites85.5%
if 10 < b Initial program 48.1%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites96.2%
Taylor expanded in b around -inf
Applied rewrites96.3%
Taylor expanded in c around 0
Applied rewrites96.3%
Taylor expanded in b around inf
distribute-lft-outN/A
associate-*r/N/A
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6488.0
Applied rewrites88.0%
Final simplification87.6%
(FPCore (a b c) :precision binary64 (if (<= b 10.0) (/ (- (sqrt (fma b b (* (* -4.0 c) a))) b) (* 2.0 a)) (/ (fma a (/ (* c c) (* b b)) c) (- b))))
double code(double a, double b, double c) {
double tmp;
if (b <= 10.0) {
tmp = (sqrt(fma(b, b, ((-4.0 * c) * a))) - b) / (2.0 * a);
} else {
tmp = fma(a, ((c * c) / (b * b)), c) / -b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 10.0) tmp = Float64(Float64(sqrt(fma(b, b, Float64(Float64(-4.0 * c) * a))) - b) / Float64(2.0 * a)); else tmp = Float64(fma(a, Float64(Float64(c * c) / Float64(b * b)), c) / Float64(-b)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 10.0], N[(N[(N[Sqrt[N[(b * b + N[(N[(-4.0 * c), $MachinePrecision] * a), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] / N[(2.0 * a), $MachinePrecision]), $MachinePrecision], N[(N[(a * N[(N[(c * c), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + c), $MachinePrecision] / (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 10:\\
\;\;\;\;\frac{\sqrt{\mathsf{fma}\left(b, b, \left(-4 \cdot c\right) \cdot a\right)} - b}{2 \cdot a}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c \cdot c}{b \cdot b}, c\right)}{-b}\\
\end{array}
\end{array}
if b < 10Initial program 84.0%
lift--.f64N/A
sub-negN/A
lift-*.f64N/A
lower-fma.f64N/A
lift-*.f64N/A
lift-*.f64N/A
associate-*l*N/A
distribute-lft-neg-inN/A
*-commutativeN/A
associate-*r*N/A
lower-*.f64N/A
lower-*.f64N/A
metadata-eval84.2
Applied rewrites84.2%
if 10 < b Initial program 48.1%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites96.2%
Taylor expanded in b around -inf
Applied rewrites96.3%
Taylor expanded in c around 0
Applied rewrites96.3%
Taylor expanded in b around inf
distribute-lft-outN/A
associate-*r/N/A
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6488.0
Applied rewrites88.0%
Final simplification87.5%
(FPCore (a b c) :precision binary64 (if (<= b 10.0) (* (- (sqrt (fma (* -4.0 c) a (* b b))) b) (/ 0.5 a)) (/ (fma a (/ (* c c) (* b b)) c) (- b))))
double code(double a, double b, double c) {
double tmp;
if (b <= 10.0) {
tmp = (sqrt(fma((-4.0 * c), a, (b * b))) - b) * (0.5 / a);
} else {
tmp = fma(a, ((c * c) / (b * b)), c) / -b;
}
return tmp;
}
function code(a, b, c) tmp = 0.0 if (b <= 10.0) tmp = Float64(Float64(sqrt(fma(Float64(-4.0 * c), a, Float64(b * b))) - b) * Float64(0.5 / a)); else tmp = Float64(fma(a, Float64(Float64(c * c) / Float64(b * b)), c) / Float64(-b)); end return tmp end
code[a_, b_, c_] := If[LessEqual[b, 10.0], N[(N[(N[Sqrt[N[(N[(-4.0 * c), $MachinePrecision] * a + N[(b * b), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - b), $MachinePrecision] * N[(0.5 / a), $MachinePrecision]), $MachinePrecision], N[(N[(a * N[(N[(c * c), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + c), $MachinePrecision] / (-b)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;b \leq 10:\\
\;\;\;\;\left(\sqrt{\mathsf{fma}\left(-4 \cdot c, a, b \cdot b\right)} - b\right) \cdot \frac{0.5}{a}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c \cdot c}{b \cdot b}, c\right)}{-b}\\
\end{array}
\end{array}
if b < 10Initial program 84.0%
lift-/.f64N/A
clear-numN/A
associate-/r/N/A
lower-*.f64N/A
lift-*.f64N/A
associate-/r*N/A
metadata-evalN/A
lower-/.f6484.1
lift-+.f64N/A
+-commutativeN/A
lift-neg.f64N/A
unsub-negN/A
lower--.f6484.1
Applied rewrites84.1%
if 10 < b Initial program 48.1%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites96.2%
Taylor expanded in b around -inf
Applied rewrites96.3%
Taylor expanded in c around 0
Applied rewrites96.3%
Taylor expanded in b around inf
distribute-lft-outN/A
associate-*r/N/A
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6488.0
Applied rewrites88.0%
Final simplification87.4%
(FPCore (a b c) :precision binary64 (/ (fma a (/ (* c c) (* b b)) c) (- b)))
double code(double a, double b, double c) {
return fma(a, ((c * c) / (b * b)), c) / -b;
}
function code(a, b, c) return Float64(fma(a, Float64(Float64(c * c) / Float64(b * b)), c) / Float64(-b)) end
code[a_, b_, c_] := N[(N[(a * N[(N[(c * c), $MachinePrecision] / N[(b * b), $MachinePrecision]), $MachinePrecision] + c), $MachinePrecision] / (-b)), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(a, \frac{c \cdot c}{b \cdot b}, c\right)}{-b}
\end{array}
Initial program 53.5%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites93.8%
Taylor expanded in b around -inf
Applied rewrites93.9%
Taylor expanded in c around 0
Applied rewrites93.9%
Taylor expanded in b around inf
distribute-lft-outN/A
associate-*r/N/A
mul-1-negN/A
lower-neg.f64N/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6483.6
Applied rewrites83.6%
Final simplification83.6%
(FPCore (a b c) :precision binary64 (/ (* (fma a (/ c (* b b)) 1.0) c) (- b)))
double code(double a, double b, double c) {
return (fma(a, (c / (b * b)), 1.0) * c) / -b;
}
function code(a, b, c) return Float64(Float64(fma(a, Float64(c / Float64(b * b)), 1.0) * c) / Float64(-b)) end
code[a_, b_, c_] := N[(N[(N[(a * N[(c / N[(b * b), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision] * c), $MachinePrecision] / (-b)), $MachinePrecision]
\begin{array}{l}
\\
\frac{\mathsf{fma}\left(a, \frac{c}{b \cdot b}, 1\right) \cdot c}{-b}
\end{array}
Initial program 53.5%
Taylor expanded in a around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
Applied rewrites93.8%
Taylor expanded in b around -inf
Applied rewrites93.9%
Taylor expanded in c around 0
Applied rewrites90.5%
Taylor expanded in c around 0
Applied rewrites83.5%
Final simplification83.5%
(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 53.5%
Taylor expanded in c around 0
associate-*r/N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6466.0
Applied rewrites66.0%
herbie shell --seed 2024278
(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)))