
(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 (/ (* (* a -2.0) (/ c a)) (+ b (sqrt (* a (fma c -4.0 (/ (pow b 2.0) a)))))))
double code(double a, double b, double c) {
return ((a * -2.0) * (c / a)) / (b + sqrt((a * fma(c, -4.0, (pow(b, 2.0) / a)))));
}
function code(a, b, c) return Float64(Float64(Float64(a * -2.0) * Float64(c / a)) / Float64(b + sqrt(Float64(a * fma(c, -4.0, Float64((b ^ 2.0) / a)))))) end
code[a_, b_, c_] := N[(N[(N[(a * -2.0), $MachinePrecision] * N[(c / a), $MachinePrecision]), $MachinePrecision] / N[(b + N[Sqrt[N[(a * N[(c * -4.0 + N[(N[Power[b, 2.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(a \cdot -2\right) \cdot \frac{c}{a}}{b + \sqrt{a \cdot \mathsf{fma}\left(c, -4, \frac{{b}^{2}}{a}\right)}}
\end{array}
Initial program 20.0%
*-commutative20.0%
+-commutative20.0%
sqr-neg20.0%
unsub-neg20.0%
sqr-neg20.0%
fma-neg20.1%
distribute-lft-neg-in20.1%
*-commutative20.1%
*-commutative20.1%
distribute-rgt-neg-in20.1%
metadata-eval20.1%
Simplified20.1%
Taylor expanded in a around inf 20.1%
flip--20.0%
add-sqr-sqrt20.5%
*-commutative20.5%
fma-define20.5%
unpow220.5%
*-commutative20.5%
fma-define20.5%
Applied egg-rr20.5%
Taylor expanded in a around 0 99.4%
*-commutative99.4%
*-commutative99.4%
Simplified99.4%
*-un-lft-identity99.4%
associate-/l/99.3%
associate-*l*99.3%
sqrt-prod99.2%
fma-define99.3%
Applied egg-rr99.3%
*-lft-identity99.3%
associate-/r*99.7%
associate-*r*99.7%
*-commutative99.7%
times-frac99.7%
associate-*r/99.7%
metadata-eval99.7%
Simplified99.7%
associate-/l*99.5%
fma-undefine99.4%
sqrt-prod99.5%
Applied egg-rr99.5%
associate-*r/99.7%
associate-*r*99.7%
*-commutative99.7%
associate-*r*99.7%
+-commutative99.7%
Simplified99.7%
(FPCore (a b c) :precision binary64 (* (* a (/ c a)) (/ -2.0 (+ b (sqrt (* a (fma c -4.0 (/ (pow b 2.0) a))))))))
double code(double a, double b, double c) {
return (a * (c / a)) * (-2.0 / (b + sqrt((a * fma(c, -4.0, (pow(b, 2.0) / a))))));
}
function code(a, b, c) return Float64(Float64(a * Float64(c / a)) * Float64(-2.0 / Float64(b + sqrt(Float64(a * fma(c, -4.0, Float64((b ^ 2.0) / a))))))) end
code[a_, b_, c_] := N[(N[(a * N[(c / a), $MachinePrecision]), $MachinePrecision] * N[(-2.0 / N[(b + N[Sqrt[N[(a * N[(c * -4.0 + N[(N[Power[b, 2.0], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(a \cdot \frac{c}{a}\right) \cdot \frac{-2}{b + \sqrt{a \cdot \mathsf{fma}\left(c, -4, \frac{{b}^{2}}{a}\right)}}
\end{array}
Initial program 20.0%
*-commutative20.0%
+-commutative20.0%
sqr-neg20.0%
unsub-neg20.0%
sqr-neg20.0%
fma-neg20.1%
distribute-lft-neg-in20.1%
*-commutative20.1%
*-commutative20.1%
distribute-rgt-neg-in20.1%
metadata-eval20.1%
Simplified20.1%
Taylor expanded in a around inf 20.1%
flip--20.0%
add-sqr-sqrt20.5%
*-commutative20.5%
fma-define20.5%
unpow220.5%
*-commutative20.5%
fma-define20.5%
Applied egg-rr20.5%
Taylor expanded in a around 0 99.4%
*-commutative99.4%
*-commutative99.4%
Simplified99.4%
*-un-lft-identity99.4%
associate-/l/99.3%
associate-*l*99.3%
sqrt-prod99.2%
fma-define99.3%
Applied egg-rr99.3%
*-lft-identity99.3%
associate-/r*99.7%
associate-*r*99.7%
*-commutative99.7%
times-frac99.7%
associate-*r/99.7%
metadata-eval99.7%
Simplified99.7%
associate-/l*99.5%
fma-undefine99.4%
sqrt-prod99.5%
Applied egg-rr99.5%
Final simplification99.5%
(FPCore (a b c) :precision binary64 (let* ((t_0 (* -4.0 (* a c)))) (/ (/ t_0 (+ b (sqrt (+ (pow b 2.0) t_0)))) (* a 2.0))))
double code(double a, double b, double c) {
double t_0 = -4.0 * (a * c);
return (t_0 / (b + sqrt((pow(b, 2.0) + t_0)))) / (a * 2.0);
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8) :: t_0
t_0 = (-4.0d0) * (a * c)
code = (t_0 / (b + sqrt(((b ** 2.0d0) + t_0)))) / (a * 2.0d0)
end function
public static double code(double a, double b, double c) {
double t_0 = -4.0 * (a * c);
return (t_0 / (b + Math.sqrt((Math.pow(b, 2.0) + t_0)))) / (a * 2.0);
}
def code(a, b, c): t_0 = -4.0 * (a * c) return (t_0 / (b + math.sqrt((math.pow(b, 2.0) + t_0)))) / (a * 2.0)
function code(a, b, c) t_0 = Float64(-4.0 * Float64(a * c)) return Float64(Float64(t_0 / Float64(b + sqrt(Float64((b ^ 2.0) + t_0)))) / Float64(a * 2.0)) end
function tmp = code(a, b, c) t_0 = -4.0 * (a * c); tmp = (t_0 / (b + sqrt(((b ^ 2.0) + t_0)))) / (a * 2.0); end
code[a_, b_, c_] := Block[{t$95$0 = N[(-4.0 * N[(a * c), $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$0 / N[(b + N[Sqrt[N[(N[Power[b, 2.0], $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(a * 2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := -4 \cdot \left(a \cdot c\right)\\
\frac{\frac{t\_0}{b + \sqrt{{b}^{2} + t\_0}}}{a \cdot 2}
\end{array}
\end{array}
Initial program 20.0%
*-commutative20.0%
+-commutative20.0%
sqr-neg20.0%
unsub-neg20.0%
sqr-neg20.0%
fma-neg20.1%
distribute-lft-neg-in20.1%
*-commutative20.1%
*-commutative20.1%
distribute-rgt-neg-in20.1%
metadata-eval20.1%
Simplified20.1%
Taylor expanded in a around inf 20.1%
flip--20.0%
add-sqr-sqrt20.5%
*-commutative20.5%
fma-define20.5%
unpow220.5%
*-commutative20.5%
fma-define20.5%
Applied egg-rr20.5%
Taylor expanded in a around 0 99.4%
*-commutative99.4%
*-commutative99.4%
Simplified99.4%
Taylor expanded in a around 0 99.4%
Final simplification99.4%
(FPCore (a b c) :precision binary64 (/ (* c (- (fma a (* c (pow b -2.0)) 1.0))) b))
double code(double a, double b, double c) {
return (c * -fma(a, (c * pow(b, -2.0)), 1.0)) / b;
}
function code(a, b, c) return Float64(Float64(c * Float64(-fma(a, Float64(c * (b ^ -2.0)), 1.0))) / b) end
code[a_, b_, c_] := N[(N[(c * (-N[(a * N[(c * N[Power[b, -2.0], $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision])), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{c \cdot \left(-\mathsf{fma}\left(a, c \cdot {b}^{-2}, 1\right)\right)}{b}
\end{array}
Initial program 20.0%
*-commutative20.0%
Simplified20.0%
Taylor expanded in c around 0 92.8%
associate-*r/92.8%
neg-mul-192.8%
distribute-rgt-neg-in92.8%
Simplified92.8%
frac-sub92.7%
distribute-rgt-neg-out92.7%
pow-plus92.6%
metadata-eval92.6%
Applied egg-rr92.6%
*-commutative92.6%
distribute-rgt-neg-in92.6%
*-rgt-identity92.6%
Simplified92.6%
Taylor expanded in b around -inf 92.8%
associate-*r/92.8%
mul-1-neg92.8%
+-commutative92.8%
associate-/l*92.8%
Simplified92.8%
associate-*r/93.1%
fma-define93.1%
div-inv93.1%
pow-flip93.1%
metadata-eval93.1%
Applied egg-rr93.1%
(FPCore (a b c) :precision binary64 (/ (- (- c) (* a (pow (/ c (- b)) 2.0))) b))
double code(double a, double b, double c) {
return (-c - (a * pow((c / -b), 2.0))) / 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 - (a * ((c / -b) ** 2.0d0))) / b
end function
public static double code(double a, double b, double c) {
return (-c - (a * Math.pow((c / -b), 2.0))) / b;
}
def code(a, b, c): return (-c - (a * math.pow((c / -b), 2.0))) / b
function code(a, b, c) return Float64(Float64(Float64(-c) - Float64(a * (Float64(c / Float64(-b)) ^ 2.0))) / b) end
function tmp = code(a, b, c) tmp = (-c - (a * ((c / -b) ^ 2.0))) / b; end
code[a_, b_, c_] := N[(N[((-c) - N[(a * N[Power[N[(c / (-b)), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / b), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(-c\right) - a \cdot {\left(\frac{c}{-b}\right)}^{2}}{b}
\end{array}
Initial program 20.0%
*-commutative20.0%
Simplified20.0%
Taylor expanded in c around 0 92.8%
associate-*r/92.8%
neg-mul-192.8%
distribute-rgt-neg-in92.8%
Simplified92.8%
frac-sub92.7%
distribute-rgt-neg-out92.7%
pow-plus92.6%
metadata-eval92.6%
Applied egg-rr92.6%
*-commutative92.6%
distribute-rgt-neg-in92.6%
*-rgt-identity92.6%
Simplified92.6%
Taylor expanded in b around inf 93.1%
neg-mul-193.1%
mul-1-neg93.1%
unsub-neg93.1%
associate-/l*93.1%
unpow293.1%
unpow293.1%
times-frac93.1%
sqr-neg93.1%
distribute-frac-neg93.1%
distribute-frac-neg93.1%
unpow293.1%
Simplified93.1%
Final simplification93.1%
(FPCore (a b c) :precision binary64 (* c (- (/ -1.0 b) (* (* a c) (pow b -3.0)))))
double code(double a, double b, double c) {
return c * ((-1.0 / b) - ((a * c) * pow(b, -3.0)));
}
real(8) function code(a, b, c)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
code = c * (((-1.0d0) / b) - ((a * c) * (b ** (-3.0d0))))
end function
public static double code(double a, double b, double c) {
return c * ((-1.0 / b) - ((a * c) * Math.pow(b, -3.0)));
}
def code(a, b, c): return c * ((-1.0 / b) - ((a * c) * math.pow(b, -3.0)))
function code(a, b, c) return Float64(c * Float64(Float64(-1.0 / b) - Float64(Float64(a * c) * (b ^ -3.0)))) end
function tmp = code(a, b, c) tmp = c * ((-1.0 / b) - ((a * c) * (b ^ -3.0))); end
code[a_, b_, c_] := N[(c * N[(N[(-1.0 / b), $MachinePrecision] - N[(N[(a * c), $MachinePrecision] * N[Power[b, -3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
c \cdot \left(\frac{-1}{b} - \left(a \cdot c\right) \cdot {b}^{-3}\right)
\end{array}
Initial program 20.0%
*-commutative20.0%
Simplified20.0%
Taylor expanded in c around 0 92.8%
associate-*r/92.8%
neg-mul-192.8%
distribute-rgt-neg-in92.8%
Simplified92.8%
div-inv92.8%
*-un-lft-identity92.8%
prod-diff92.8%
distribute-rgt-neg-out92.8%
pow-flip92.8%
metadata-eval92.8%
Applied egg-rr92.8%
+-commutative92.8%
fma-undefine92.8%
*-rgt-identity92.8%
associate-+r+92.8%
distribute-lft-neg-out92.8%
unsub-neg92.8%
fma-undefine92.8%
distribute-rgt-out92.8%
+-commutative92.8%
sub-neg92.8%
+-inverses92.8%
metadata-eval92.8%
neg-sub092.8%
Simplified92.8%
Final simplification92.8%
(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 20.0%
*-commutative20.0%
Simplified20.0%
Taylor expanded in b around inf 88.3%
associate-*r/88.3%
mul-1-neg88.3%
Simplified88.3%
Final simplification88.3%
herbie shell --seed 2024105
(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)))